Commit 95b2d584 authored by Farid Neshat's avatar Farid Neshat

Initial commit

parents
root = true
[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
* text=auto
node_modules
coverage
language: node_js
node_js:
- 7
- 6
- 4
after_script: 'cat ./coverage/lcov.info | coveralls'
{
"generator-node": {
"promptValues": {
"authorName": "Farid Neshat",
"authorEmail": "FaridN_SOAD@yahoo.com",
"authorUrl": ""
}
}
}
\ No newline at end of file
The MIT License (MIT)
Copyright (c) 2017 Olindata BV
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# 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
## Installation
```sh
$ npm install --save lite-lru
```
## Usage
```js
const LiteLRU = require('lite-lru');
const size = 5;
const lru = new LiteLRU(size);
const key = lru.add('any value');
// Later you can get back the value:
lru.get(key); // > 'any value'
lru.delete(key);
lru.get(key); // > undefined
```
There's a `toJSON` method defined which basically returns the internal
array. You can pass that array to LiteLRU constructor:
```
new LiteLRU(50, JSON.parse(JSON.stringify(new LiteLRU(50))))
```
You can set callback for when a value is added, so that you can save
the cache somewhere:
```
lru.onAdd = () => {
localStorage.cache = JSON.stringify(lru);
}
```
## License
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
const assert = require('assert');
const LiteLRU = require('../index.js');
describe('LiteLRU', () => {
it('add', () => {
const lru = new LiteLRU(3);
const something = 'something';
const i = lru.add(something);
assert.equal(lru.get(i), something);
assert.equal(lru.get(i + 1), undefined);
lru.add();
lru.add();
lru.add();
assert.equal(lru.get(i), undefined);
});
it('serialization', () => {
const something = 'something';
let lru = new LiteLRU(3);
const i = lru.add(something);
lru = new LiteLRU(3, JSON.parse(JSON.stringify(lru)));
assert.equal(lru.get(i), something);
});
it('delete', () => {
const lru = new LiteLRU(3);
const something = 'something';
const i = lru.add(something);
lru.delete(i);
assert.equal(lru.get(i), undefined);
});
it('Takes minimum space when serialized', () => {
let lru = new LiteLRU(3);
for (let i = 0; i < 5; i++) {
lru.add();
lru.delete();
}
assert.equal(JSON.stringify(lru).length, 2);
});
it('Calls handler on add', () => {
let called;
let lru = new LiteLRU(3);
lru.onAdd = () => {
called = true;
};
lru.add();
assert.ok(called);
});
});
'use strict';
const bs = require('binarysearch');
const findKey = (array, key) => bs(array, key, (value, find) => value.key - find);
class LiteLRU {
constructor(n, 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});
if (this.array.length > this.max) {
this.array.shift();
}
if (this.onAdd) {
this.onAdd();
}
return this.lastKey;
}
get(key) {
const i = findKey(this.array, key);
if (i !== -1) {
return this.array[i].data;
}
}
delete(key) {
const i = findKey(this.array, key);
if (i !== -1) {
this.array.splice(i, 1);
}
}
toJSON() {
return this.array;
}
}
module.exports = LiteLRU;
This diff is collapsed.
{
"name": "lite-lru",
"version": "0.0.0",
"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": {
"name": "Farid Neshat",
"email": "FaridN_SOAD@yahoo.com",
"url": ""
},
"files": [
"lib"
],
"main": "lib/index.js",
"keywords": [
"lru",
"cache",
"light",
"lite",
"serialization",
"integer",
"binary"
],
"devDependencies": {
"coveralls": "^2.13.1",
"nsp": "^2.6.3",
"eslint": "^4.1.0",
"eslint-config-xo-space": "^0.16.0",
"jest": "^21.0.0",
"jest-cli": "^21.0.0"
},
"scripts": {
"prepublish": "nsp check",
"pretest": "eslint . --fix",
"test": "jest"
},
"eslintConfig": {
"extends": "xo-space",
"env": {
"jest": true,
"node": true
}
},
"repository": {
"type": "git",
"url": "https://gitlab.olindata.com/olindata/node-lite-lru.git"
},
"jest": {
"testEnvironment": "node"
},
"license": "MIT",
"dependencies": {
"binarysearch": "^1.0.0"
}
}
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