Registry / storage / levelup-cache

levelup-cache

JSON →
library2.3.0jsnpmunverified

Cache remote data in a LevelUP database with automatic refresh and TTL. Version 2.3.0 is the current stable release. Key features: returns cached value immediately regardless of age, automatically calls the getter periodically to check for new values, emits events when changes are detected, and drops items after a configurable TTL since last access. Unlike level-ttl-cache, this library refreshes values even if the key is not accessed, and emits change events. Breaking change in 2.0.0: switched from sublevel to subleveldown. Maintained by TehShrike, low activity.

npm install levelup-cache
INSTALL
IMPORT
SIG · LEVELUP-CACHE
L
levelup-cache
storagejavascriptv2.3.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Cache
import Cache from 'levelup-cache'
const { Cache } = require('levelup-cache')
Default export is the constructor. Use named import only if using named exports (not default).
default
const Cache = require('levelup-cache')
const cache = require('levelup-cache').Cache
CommonJS require returns the constructor function directly.
Cache (TypeScript)
import Cache = require('levelup-cache')
import { Cache } from 'levelup-cache'
TypeScript with esModuleInterop: use default import if enabled, otherwise use import = require.

Basic setup: create LevelUP database, define a getter function, instantiate Cache, get a value, listen for changes, and stop the cache.

const levelup = require('levelup') const leveldown = require('leveldown') const Cache = require('levelup-cache') const db = levelup(leveldown('./cache.db')) const getter = (key, cb) => { // simulate remote fetch setTimeout(() => cb(null, { id: 1, content: 'data' }), 100) } const cache = new Cache(db, getter, { refreshEvery: 10000, checkToSeeIfItemsNeedToBeRefreshedEvery: 5000, ttl: 86400000 }) cache.get('mykey', (err, val) => { console.log('cached value:', val) }) cache.on('change', (key, val, prev) => { console.log('value changed for', key) }) setTimeout(() => cache.stop(), 30000)
Debug
Known issues
breakingVersion 2.0.0 switched from sublevel to subleveldown. Existing caches using sublevel will not be compatible.
fix
Upgrade to 2.x and ensure your database is migrated or recreated with subleveldown.
affects: <2.0.0
deprecatedThe 'change' event may be deprecated in future versions; prefer using the 'changed' event if introduced.
fix
Monitor changelog and migrate to any new event names.
affects: >=2.0.0
gotchaThe getter callback must be called exactly once per invocation, otherwise cache may hang.
fix
Ensure your getter calls cb(err, value) exactly once.
affects: >=1.0.0
gotchaComparison function defaults to strict equality (===) which fails for objects. Provide custom comparison for object values to detect changes.
fix
Pass a comparison function that deep-compares values, e.g., using lodash.isEqual.
affects: >=1.0.0
Errors
Common errors & fixes
Error: db.get is not a function
LevelUP instance not passed correctly; db is not a LevelUP object.
fix
Ensure you create a LevelUP instance with a LevelDOWN backend, e.g., const db = levelup(leveldown('./path'))
TypeError: Cannot read property 'on' of undefined
Cache constructor called without new or not receiving a valid LevelUP instance.
fix
Use new Cache(db, getter, options) or ensure db is a LevelUP instance.
AssertionError [ERR_ASSERTION]: TypeError [ERR_INVALID_ARG_TYPE]: The "db" argument must be an instance of LevelUP
Passing an abstract-leveldown database instead of LevelUP.
fix
Use levelup(dbdown) to wrap your leveldown instance.
Upgrade
Version history
2.3.0latest on npm
Audit
Dependencies
leveluprequiredrequired as the database store
subleveldownrequiredused for sublevel support since version 2.0.0
Agent activity
15 hits · last 30 days
node
14
Resources
levelup-cache — npm install levelup-cache · libregistry