This package has been deprecated

Author message:

no longer maintained

level-ttl-cache

2.2.3 • Public • Published

LevelTTLCache

A pass-through cache for arbitrary objects or binary data using LevelDB, expired by a TTL


NPM NPM

Requires LevelUP (or Level) and sublevel to be installed separately.

Also builds on LevelTTL to provide TTL-based cache timeouts, you'll need to initialise it too. You can also set a custom check-frequency when you initialise LevelTTL.

Note that none of these libraries are included in the dependencies of LevelTTLCache so you'll need to install them and set them up prior to initialising a LevelTTLCache instance.

const level    = require('level')
    , sublevel = require('level-sublevel')
    , ttl      = require('level-ttl')
    , Cache    = require('level-ttl-cache')
 
var db = level('/my/big/cache.db')
 
db = sublevel(db) // setup sublevel
db = ttl(db)      // setup level-ttl, set checkFrequency here if desired
 
// you can create as many caches as you need and they can live separately
var cache = Cache({
    db   : db
  , name : 'mybigcache'  // should be unique per cache, used to make a sublevel
  , ttl  : 1000 * 60 * 5 // 5 minute ttl for all entries in this cache
  , load : function (key, callback) {
      // do some (possibly async) work to load the value for `key`
      // and return it as the second argument to the callback,
      // the first argument should be null unless there is an error
      callback(null, value)
    }
})
 
// our cache now has a `get` method that operates like a simple read-only
// key/value store but whose entries are kept for a certain period of time
// in a leveldb instance, after which they expire and need to be re-loaded
// from our load() function
 
cache.get('foo', function (err, value) {
  // `value` may or may not have come via our load() function, depending on
  // whether load() was called for this key within the last 5 minutes and
  // successfully returned a value
})

If you are doing anything but UTF8 then you may want to pass in a 'valueEncoding' option to set what encoding you want to explicitly set on put() and get() requests. You can also give a 'keyEncoding' option if required.

The cache doesn't require an explicit shut-down, instead you ought to close() the original db instance, which will halt the LevelTTL check cycle and close the underlying LevelDB store.

Licence & copyright

Copyright (c) 2013 Rod Vagg.

LevelTTLCache is licensed under an MIT +no-false-attribs license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.

Package Sidebar

Install

npm i level-ttl-cache

Weekly Downloads

0

Version

2.2.3

License

MIT

Last publish

Collaborators

  • rvagg