lru-cache-object

0.1.2 • Public • Published

Build Status License: GPL v3

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(/* lru-cache configurtion options */);
o.cat = 'meow';
console.log(o.cat); // ==> 'meow'
 
const o2 = lru(2); // init with 2 item max
o2.cat = 'meow';
o2.dog = 'woof';
o2.pig = 'oink';
console.log(Object.keys(o2)); // ==> ['pig', 'dog']

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

Package Sidebar

Install

npm i lru-cache-object

Weekly Downloads

1

Version

0.1.2

License

MIT

Unpacked Size

40.2 kB

Total Files

8

Last publish

Collaborators

  • dve