Skip to content

austinkelleher/in-mem-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

JavaScript in memory cache that supports reaping

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published