Registry / storage / cache-base

cache-base

JSON →
library4.0.2jsnpmunverified

Basic object cache with `get`, `set`, `del`, and `has` methods for Node.js/JavaScript projects. Version 4.0.2 (latest stable, 2023-09) provides a simple in-memory cache with event emission and support for dot-notation keys. It is part of the 'base' ecosystem and is dependency-free. Unlike more complex caching solutions, it offers minimal overhead and is ideal for small-scale or temporary caching needs. The release cadence is low (sporadic updates), and it is considered stable and maintained.

npm install cache-base
INSTALL
IMPORT
SIG · CACHE-BASE
C
cache-base
storagejavascriptv4.0.2
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.

CacheBase
const CacheBase = require('cache-base');
const CacheBase = require('cache-base').CacheBase;
cache-base is a CommonJS module; the default export is the constructor. No named exports.
instance methods (set, get, has, del, prime, default)
const cache = new CacheBase(); cache.set('key', 'value');
CacheBase.set('key', 'value');
Methods are available on instances, not the constructor. Always instantiate before use.
EventEmitter pattern (optional)
const cache = new CacheBase(); cache.on('set', (key, val) => {});
CacheBase.on('set', ...);
CacheBase inherits from EventEmitter; you can listen for 'set', 'get', 'del', etc. on the instance.

Demonstrates basic usage: creating a cache, setting/getting with dot-notation, checking existence, deleting, setting default values, listening to events, and the prime method.

const CacheBase = require('cache-base'); const cache = new CacheBase(); cache.set('a.b', 'c'); console.log(cache.get('a.b')); // 'c' console.log(cache.has('a')); // true cache.del('a.b'); console.log(cache.get('a')); // {} // Using default values cache.default('color', 'red'); console.log(cache.get('color')); // 'red' cache.set('color', 'blue'); console.log(cache.get('color')); // 'blue' // Listen for events cache.on('set', (key, val) => console.log(`set ${key} to ${val}`)); cache.set('lang', 'js'); // logs "set lang to js" // Prime only sets if not already defined cache.prime('lang', 'ts'); console.log(cache.get('lang')); // still 'js'
Debug
Known issues
gotchaDot-notation keys that correspond to existing prototype properties (e.g., 'toString', 'hasOwnProperty') may overwrite or conflict with built-in object methods.
fix
Avoid using keys like 'toString', 'constructor', etc. Sanitize keys if they come from untrusted sources.
affects: >=1.0.0
gotchaThe cache object is exposed via `cache.cache`. Modifying it directly bypasses event emission and might cause unexpected behavior.
fix
Always use `set`, `del`, `prime` etc. to manipulate the cache instead of directly altering `cache.cache`.
affects: >=1.0.0
gotcha`default` values are stored in a separate `defaults` object. Setting a value with `set` does not remove the default; `get` returns the set value if exists, else the default.
fix
Understand that defaults are only consulted when the key is absent from the cache. To clear a default, use `app.del(key)` or set the key to undefined.
affects: >=1.0.0
deprecatedThe `set` method accepts arrays but behavior may be confusing: it merges each object into the cache rather than treating the array as a key.
fix
Pass a single object or key-value pair; avoid passing arrays unless you intend to merge multiple objects.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: cache_base__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor
Using ES module import with CommonJS module that has no default export; webpack may mis-handle.
fix
Use `const CacheBase = require('cache-base').default` or configure webpack to handle CommonJS properly.
Cannot find module 'cache-base'
Package not installed or import path incorrect.
fix
Run `npm install cache-base` and ensure `require('cache-base')` is used (no subpath).
app.on is not a function
Trying to listen to events on an instance that is not an EventEmitter (older version?).
fix
Ensure you are using cache-base v4.0.2 or later, or check that cache-base is correctly instantiated.
Upgrade
Version history
4.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
21 hits · last 30 days
node
18
OpenAI (training)
1
Resources
cache-base — npm install cache-base · libregistry