lru-cache-object
A simple proxy to lru-cache that creates plain javascript objects that behave like an lru cache.
Accepts the same configuration options as lru-cache
(What is an LRU cache?)
example
basic
const lru = require('lru-cache-object');
const o = lru();
o.cat = 'meow';
console.log(o.cat);
const o2 = lru(2);
o2.cat = 'meow';
o2.dog = 'woof';
o2.pig = 'oink';
console.log(Object.keys(o2));
recipes
with a redux reducer
With some caveats its possible to use it as lru cache reducer
export const myReducer = (state = { items: lru(100) }, action) => {
if (action.type === 'SOME_ACTION') {
return {
...state,
items: Object.assign(state.items, action.payload),
}
}
return state;
};
caveats:
- can not use spread operator
- this essentially modifies the original object which is a bit of a best practice no-no
- if you data is so large that you need this, it maybe be an indicator of a bigger architectural issue
license
GNUv3