
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
memory-cacheable
Advanced tools
minimal in memory completely volatile cache store with a "stale concept"
I built this because I had the need to store expensive and frequently called functions into memory. It's not a framework for caches, it's not really a class, or a cache wrapper. It's minimal, and encourages you to decide how you want to interact with the available states of your cached collections. Think of this as a function that keeps state of points in time, with a couple helpers to manage cached collections.
npm install --save memory-cacheable
cacheable({ key, cache, ttl, offset, verbose })
see below table for options:
key | required | summary |
---|---|---|
key | false | key for debugging purposes, mostly |
ttl | false | defaults to 1 hour, uses the ms module for easy formatting |
cache | false | defaults to an array, but will also support cleaning out objects |
offset | false | defaults to 0.65 (roughly 2/3 the ttl ), used to create the stale value by multiplying the ttl by offset |
verbose | false | defaults to false turn this on for really noisey cache stats and debugging |
function | summary |
---|---|
cacheable(store).debug() | debug logging, noisey, expensive stuff |
cacheable(store).expired() | check if cache state is expired |
cacheable(store).fetch() | fetches the cached collection |
cacheable(store).flush() | flush cache, supports Objects and Arrays |
cacheable(store).set(collection) | sets cached collection from provided param |
cacheable(store).size() | gets the cache size, if an object Object.keys() is called |
cacheable(store).stale() | determines if the cache is stale or not |
cacheable(store).state() | returns whether the cache is expired or not, if cache is expired, state will reset and flush the cache |
cacheable(store).reset() | restarts timers, will not flush cache |
const { cacheable } = require('memory-cacheable');
const store = {
key: 'expensiveCacheExample',
ttl: '1h'
};
const expired = cacheable(store).state; // pulls state out to manage state and check if expired
const { stale, reset, flush } = cacheable(store); // other helpful tings
// simplest example usage
function doSomethingExpensive(fn) {
if (!expired()) return fn(null, store.cache);
return funcToExpensiveThings((err, resp) => {
if (err) return fn(new Error(err), null);
store.cache = resp;
return fn(null, resp);
});
}
// example usage for expensive or long running functions with
// stale support
function doSomethingExpensive(fn) {
const isStale = stale();
if (!expired() && !isStale) return fn(null, store.cache);
if (isStale) fn(null, store.cache);
return funcToExpensiveThings((err, resp) => {
if (err) return fn(new Error(err), null);
store.cache = resp;
if (isStale) return reset();
return fn(null, resp);
});
}
Uses the debug
module, and you can set the following environment variable:
export DEBUG=cacheable:*
npm install --save-dev && npm test
FAQs
minimal in memory completely volatile cache store with a "stale concept"
The npm package memory-cacheable receives a total of 1 weekly downloads. As such, memory-cacheable popularity was classified as not popular.
We found that memory-cacheable demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.