r-memory-cache
Installation
npm install r-memory-cache --save
Usage
cache.type // 'LRU'cache.capacity // 2cache.length // 0 cache.put'key', 'value'cache.get'key' // 'value'cache.length // 1 cache.del'key'cache.get'key' // undefined cache.put'key1', 'value1'cache.put'key2', 'value2'cache.put'key3', 'value3' cache.get'key1' // undefinedcache.get'key2' // 'value2'cache.get'key3' // 'value3' // empty the cachecache.clearcache.length // 0
API
createCache(type[, options])
type:
- 'LRU'
- 'LFU'
- 'FIFO'
options:
put(key, value)
The put
method adds or updates an entry.
cache.put1, 1
get(key)
The get
method returns a specified entry by key.
cache.put1, 1cache.get1 // 1
del(key)
The del
method removes the specified entry by key.
cache.put1, 1cache.get1 // 1cache.del1cache.get1 // undefined
keys()
The keys
method returns a array that contains the keys for each entry in the cache instance.
cache.put1, 1cache.put'a', 'a'cache.keys // [1, 'a']
clear()
The clear
method removes all entries.
cache.put1, 1cache.put2, 2cache.clearcache.get1 // undefinedcache.get2 // undefined
length
The length
property returns the number of entries in a cache instance.
cache.length // 0cache.put1, 1cache.length // 1
capacity
The capacity
property returns the largest amount of entries that can be contained in a cache instance.
cache.capacity // 64 cache2.capacity // 2cache2.put1, 1cache2.put2, 2cache2.put3, 3cache2.get1 // undefinedcache2.get2 // 2cache2.get3 // 3