Commit 0eb746a4 authored by Farid Neshat's avatar Farid Neshat

Add set

parent 95b2d584
# lite-lru [![NPM version][npm-image]][npm-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]
> A simple LRU cache module that is optimized for frequent serialization, only supports increasing integer keys and uses binary search to locate the entries
# lite-lru [![NPM version][npm-image]][npm-url]
> A simple LRU cache module that is optimized for frequent serialization, only supports number keys and uses binary search to locate the entries
## Installation
......@@ -18,10 +18,13 @@ const key = lru.add('any value');
// Later you can get back the value:
lru.get(key); // > 'any value'
lru.set(key, 'some other value');
lru.delete(key);
lru.get(key); // > undefined
```
Note that `get`, `set` and `delete` methods use binary search
There's a `toJSON` method defined which basically returns the internal
array. You can pass that array to LiteLRU constructor:
......@@ -45,7 +48,3 @@ MIT © [Olindata BV](https://www.olindata.com)
[npm-image]: https://badge.fury.io/js/lite-lru.svg
[npm-url]: https://npmjs.org/package/lite-lru
[daviddm-image]: https://david-dm.org/alFReD-NSH/lite-lru.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/alFReD-NSH/lite-lru
[coveralls-image]: https://coveralls.io/repos/alFReD-NSH/lite-lru/badge.svg
[coveralls-url]: https://coveralls.io/r/alFReD-NSH/lite-lru
......@@ -30,8 +30,8 @@ describe('LiteLRU', () => {
it('Takes minimum space when serialized', () => {
let lru = new LiteLRU(3);
for (let i = 0; i < 5; i++) {
lru.add();
lru.delete();
const a = lru.add();
lru.delete(a);
}
assert.equal(JSON.stringify(lru).length, 2);
});
......@@ -44,4 +44,15 @@ describe('LiteLRU', () => {
lru.add();
assert.ok(called);
});
it('set', () => {
let lru = new LiteLRU(3);
const id = lru.add(1);
lru.set(id, 2);
assert.equal(lru.get(id), 2);
lru.set(10, 3);
lru.set(5, 4);
assert.equal(lru.get(5), 4);
assert.equal(lru.get(10), 3);
});
});
'use strict';
const assert = require('assert');
const bs = require('binarysearch');
const findKey = (array, key) => bs(array, key, (value, find) => value.key - find);
const comparator = (value, find) => {
return (value.key || value) - (find.key || find);
};
const findKey = (array, key) => bs(array, key, comparator);
const assertKey = key => {
assert(key != null, 'Key is not provided');
assert(typeof key === 'number', 'Key is not a number');
};
class LiteLRU {
constructor(n, array = []) {
constructor(n, array) {
array = array || [];
this.max = n;
this.array = array;
this.lastKey = array.length ? array[array.length - 1].key : 0;
}
add(data) {
this.array.push({key: ++this.lastKey, data});
add(value) {
this.array.push({key: ++this.lastKey, value});
if (this.array.length > this.max) {
this.array.shift();
}
......@@ -22,13 +31,20 @@ class LiteLRU {
}
get(key) {
assertKey(key);
const i = findKey(this.array, key);
if (i !== -1) {
return this.array[i].data;
return this.array[i].value;
}
}
set(key, value) {
assertKey(key);
bs.insert(this.array, {key, value}, {unique: true}, comparator);
}
delete(key) {
assertKey(key);
const i = findKey(this.array, key);
if (i !== -1) {
this.array.splice(i, 1);
......@@ -41,3 +57,4 @@ class LiteLRU {
}
module.exports = LiteLRU;
module.exports.default = LiteLRU;
{
"name": "lite-lru",
"version": "0.0.0",
"version": "0.0.1",
"description": "A simple LRU cache module that is optimized for frequent serialization, only supports increasing integer keys and uses binary search to locate the entries",
"homepage": "https://gitlab.olindata.com/olindata/node-lite-lru",
"author": {
......@@ -39,6 +39,10 @@
"env": {
"jest": true,
"node": true
},
"rules": {
"no-eq-null": "off",
"eqeqeq": ["error", "smart"]
}
},
"repository": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment