Random Replacement Cache with TTL and preload option. A simple and clean RR cache that just works.
See Cache Types for a comparison of different cache algorithms
npm i rrcache
var defaultOptions = { max: 100000 } // max entries
var rrcache = require('rrcache')(defaultOptions)
value = rrcache.get('keyvalue')
optional ttl (time to live)
setting a key without a ttl will clear any current timeout
rrcache.set('project', 'rrcache') // will automatically make room
or
rrcache.set('mykey', 'myvalue', 60000) // expire key in 60 seconds
clear the cache
rrcache.clear()
change the cache options, currently only can change the max threshold. If the max
cache size is decreased, the cache will automatically clean the cache to the new size. max = 0
disables the RR cleaning of the cache.
rrcache.options({max: 0}) // disable cache
return the number of entries in the cache
var currentEntries = rrcache.length()
return the current key/values of the cache
rrcache.set('firstbase', 'jon')
rrcache.set('secondbase', 'joe')
var values = rrcache.values()
console.log(values)
{
"firstbase": "jon",
"secondbase": "joe"
}
or persist the cache values for later
fs = require('fs')
fs.writeFileSync('mycache.json', JSON.stringify(rrcache.values()))
manually load the cache with a set of values
fs = require('fs')
rrcache.load(JSON.parse(fs.readFileSync('mycache.json')))
loadFile()
rrcache.loadFile('mycache.json')
saveFile()
rrcache.saveFile('mycache.json')
// or if previous loadFile
rrcache.saveFile()
preload the cache with a set of values
fs = require('fs')
var initialData = JSON.parse(fs.readFileSync('mycache.json'))
var rrcache = require('rrcache')({ data: initialData }) // preload cache
reload the cache during init
var rrcache = require('rrcache')({ file: 'mycache.json', max: 1000000})
var RRCache = require('rrcache')
var cache1 = new RRCache()
var cache2 = new RRCache()