simple LRU cache atop LevelUp
npm install level-lru-cache``javascript
var Cache = require('level-lru-cache')
var levelup = require('levelup')
var memdown = require('memdown')
// Any sane LevelDOWN will do.
var level = levelup({db: memdown})
// A loose limit for how many keys the cache should store.
var limit = 100
var cache = new Cache(level, limit)
cache.put('string key', value, function(error) {
/ ... /
})
cache.get('string key', function(error, value) {
/ Value is undefined if the cache doesn't have a value. /
})
`
The cache stores values as records. Each record's key combines the
cache key and a timestamp. On put, the cache counts stored records,get
compares to the cache limit, if any, and deletes records for the oldest
keys as it adds a record for the new value. On , the cache finds
all the records for the key, deletes them all, notes the most recent
value, creates a new record for the value, and returns the value.
The general idea is to avoid issues where:
1. A first put performs LevelUP read operations. They indicate the'x'
cache is about to bust its limit, and that key is the oldest.
2. Another operation reads or writes the cached value for 'x'.
3. The first put operation performs its LevelUP write operations,'x'`, which should actually be the most recently used
deleting
value in the cache.
The use of timestamps isn't ideal, but it probably works when the
asynchronous clients of the underling LevelUP are on the same machine.