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.

storage
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.

Default export is the constructor. Use named import only if using named exports (not default).

import Cache from 'levelup-cache'

CommonJS require returns the constructor function directly.

const Cache = require('levelup-cache')

TypeScript with esModuleInterop: use default import if enabled, otherwise use import = require.

import Cache = require('levelup-cache')

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 footguns
breakingVersion 2.0.0 switched from sublevel to subleveldown. Existing caches using sublevel will not be compatible.
deprecatedThe 'change' event may be deprecated in future versions; prefer using the 'changed' event if introduced.
gotchaThe getter callback must be called exactly once per invocation, otherwise cache may hang.
gotchaComparison function defaults to strict equality (===) which fails for objects. Provide custom comparison for object values to detect changes.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
0 hits · last 30 days

No traffic data recorded yet.

Resources