simple.cache
Version:
a simple lru cache
53 lines (31 loc) • 1.8 kB
Markdown
# simple.cache
a simple lru cache
----
<a href="https://nodei.co/npm/simple.cache/"><img src="https://nodei.co/npm/simple.cache.png?downloads=true"></a>
[](https://travis-ci.org/joaquimserafim/simple.cache)[](https://coveralls.io/github/joaquimserafim/simple.cache)[](https://github.com/joaquimserafim/simple.cache/blob/master/LICENSE)[](https://github.com/joaquimserafim/simple.cache/blob/master/package.json#L39)
[](https://github.com/feross/standard)
### api
`const SimpleCache = require('simple.cache')`
`SimpleCache(max)` **max** integer, default to 1000
#### methods
* **set(key, value)** void, set a new entry and value
* **get(key)** string or undefined, gets the entry's value from a given key
* **has(key)** boolean, check if a given key already exists
* **remove(key)** void, remove a given key from the first cache
* **size()** integer, get the size of the cache
* **clear()** void, clear cache
* **keys()** array, returns the keys from the cache
* **dump()** object, retrieves the entire cache
### example
```js
const SimpleCache = require('simple.cache')
const myCache = SimpleCache()
myCache.set('foo', 123)
myCache.set('bar', 456)
myCache.get('bar')// returns 456
myCache.remove('foo')
myCache.size()// should return 1
myCache.clear()// both caches are reset
```
props to Dominic Tarr for the amazing LRU algorithm
#### ISC License (ISC)