Memory Cache
Instantiates a mutable object cache in memory. Uses Map
and Symbol
primitives, so it will only work on modern browsers (versions >= 2015), and node 4+. See benchmarks here.
Note about previous versions:
If you're using previous versions, You can find v1 documentation here and v2 documentation here and you can install using memory-cache2@1
and memory-cache2@2
. I have dropped support for functionality like getIn
, setIn
, concatIn
, mergeIn
. Instead, I opted refactoring to the more performant JS maps, only allowing merge
and concat
, and adding other features like each
, has
, and size
. I also refactored the event emitter to only allow observing and not triggering. Triggering should only occur internally, as the cache does things.
API
Installation
npm install --save memory-cache2
Usage
Instantiate
You can either pass an existing object and convert it into a cache, or you can start a brand new cache.
const MemoryCache = ;// Empty cacheconst cache = ;// or// Pass obj as cacheconst cache = myObj || ;
Set
Defines key value. Returns value.
cache;
Get
Returns key value.
const val = cache;// > { set: true, arr: [1,2,3] }const entireCache = cache;// > { testing: { set: true, arr: [1,2,3] }}
Remove
Remove key from cache.
cache;// > true
Reset
Resets cache object to empty object.
cache;// > {}
Expire
Expire cache after a certain amount of time in milliseconds.
cache;// Expires in 3 seconds
Set and Expire
Set the cache and expire after a certain amount of time in miliseconds.
cache;// > { value: 1 }// Expires in 3 seconds
Merge
Merge objects stored in cache. Works with arrays or objects only.
const val = cache;// > { set: true, arr: [1,2,3], merged: true }
Concat
Concatenates cached array.
cache;cache;// > ['a', 'b', 'c', 1, 2, 3];
Has
Checks to see if cache has a key
cache// > truecache// > false
Each
Iterates through cache.
cache;
Keys
Gets all cached keys
cachekeys;// > ['testing', 'array']
Size
Get size of cache
cachesize;// > 2
Debug
Turns logging off or on.
// Instantiate cache with debuggingconst cache = {}debug: true;// Set debugging after instantiationcachedebug = true || false;cache;// MemoryCache: set testing { set: true }cache;// MemoryCache: get testing
Events
MemoryCache events can be observed. All events return an object with at least key
and value
, except for reset
. You can bind or unbind to any event using the following API:
cache.onGet((obj) => {})
: Binds a function toget
event.cache.oneGet((obj) => {})
: Binds a function 1 time toget
event.cache.offGet(/* instantiated fn */)
: Remove a function fromget
event.cache.onSet((obj) => {})
: Binds a function toset
event.cache.oneSet((obj) => {})
: Binds a function 1 time toset
event.cache.offSet(/* instantiated fn */)
: Remove a function fromset
event.cache.onRemove((obj) => {})
: Binds a function toremove
event.cache.oneRemove((obj) => {})
: Binds a function 1 time toremove
event.cache.offRemove(/* instantiated fn */)
: Remove a function fromremove
event.cache.onExpire((obj) => {})
: Binds a function toexpire
event.cache.oneExpire((obj) => {})
: Binds a function 1 time toexpire
event.cache.offExpire(/* instantiated fn */)
: Remove a function fromexpire
event.cache.onMerge((obj) => {})
: Binds a function tomerge
event.cache.oneMerge((obj) => {})
: Binds a function 1 time tomerge
event.cache.offMerge(/* instantiated fn */)
: Remove a function frommerge
event.cache.onConcat((obj) => {})
: Binds a function toconcat
event.cache.oneConcat((obj) => {})
: Binds a function 1 time toconcat
event.cache.offConcat(/* instantiated fn */)
: Remove a function fromconcat
event.cache.onReset((obj) => {})
: Binds a function toreset
event.cache.oneReset((obj) => {})
: Binds a function 1 time toreset
event.cache.offReset(/* instantiated fn */)
: Remove a function fromreset
event.
Example:
// Instantiate cache with event bindingconst cache = {}events: true;// Set event binding after instantiationcacheevents = true || false;// Happens on every getcache;// Only happens one timecache;
Contributing
- Include 100% test coverage.
- Must support older browsers.
- Submit an issue first for significant changes.