ref-counted-cache

2.0.2 • Public • Published

ref-counted-cache

npm install --save ref-counted-cache

RefCountedCache

import RefCountedCache from 'ref-counted-cache';

class Entry {
  constructor(arg0) {
    this.text = arg0;
    console.info('created');
  }

  close() {
    this.text = 'closed';
    console.info('closed');
  }
}

const cache = new RefCountedCache({
  defaultTimeout: 5 * 1000,

  // new instance
  create: (key, arg0) => {
    return new Entry('hello world ' + arg0);
  },

  // cleanup
  clean: (entry) => {
    entry.close();
  },
});

const cacheKey = 'key1'

// calls create()
const get1 = cache.get(cacheKey, {}, 'my text 1');

// 'hello world: my text 1'
console.info(get1.value.text);

// returns cached value from first get()
const get2 = cache.get(cacheKey, {}, 'my text 2');

// true
console.info(get1.value.text === get2.value.text);

// unref both, after defaultTimeout will call clean()
get1.clean();
get2.clean();

// after defaultTimeout, clean() called for cached entry
setTimeout(() => {
  console.info(get1.value.text === 'closed');
}, 6 * 1000);

disable clean timeout

const cache = new RefCountedCache({
  defaultTimeout: 0,

  // new instance
  create: (key, arg0) => {
    return new Entry('hello world ' + arg0);
  },

  // cleanup
  clean: (entry) => {
    entry.close();
  },
});

const cacheKey = 'key1'

// calls create()
const get1 = cache.get(cacheKey, {}, 'my text 1');

// calls clean() instantly
get1.clean();

console.info(get1.value.text === 'closed');

Package Sidebar

Install

npm i ref-counted-cache

Weekly Downloads

1

Version

2.0.2

License

MIT

Unpacked Size

8.36 kB

Total Files

6

Last publish

Collaborators

  • carlhopf