in-mem-cache

1.0.3 • Public • Published

in-mem-cache

JavaScript in memory cache that supports reaping

Installation

npm install in-mem-cache --save

Usage

const InMemoryCache = require('in-mem-cache');
 
const cache = InMemoryCache.create();
const myKey = 'hello';
 
cache.put(myKey, 'world');
let value = cache.get(myKey); // 'world'

The cache will emit an event when cache entries are put and deleted if the emitEntryChanges option is set;

const cache = InMemoryCache.create({ emitEntryChanges: true });
 
cache.on(`put:${myKey}`, (data) => {
  // data === the entryData that was initially inserted
});
 
cache.on(`delete:${myKey}`, (data) => {
  // data === the entryData that was initially inserted
});
 
cache.put(myKey, myData); // triggers the put listener
cache.delete(myKey); // triggers the delete listener

Alternatively, the option can also be set as an option when values are being set.

const cache = InMemoryCache.create();
 
cache.on(`put:${myKey}`, (data) => {
  // data === the entryData that was initially inserted
});
 
cache.on(`delete:${myKey}`, (data) => {
  // data === the entryData that was initially inserted
});
 
cache.put(myKey, myData, { emitEntryChanges: true }); // triggers the put listener
cache.delete(myKey); // triggers the delete listener

Note: Setting the option on the entry will override the default cache option. This allows for finer control over what entries the cache will (or will not) emit events for.

Readme

Keywords

Package Sidebar

Install

npm i in-mem-cache

Weekly Downloads

0

Version

1.0.3

License

MIT

Last publish

Collaborators

  • austinkelleher
  • charlieduong94