Security News
OpenJS: “XZ Utils Cyberattack Likely Not an Isolated Incident”
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.
a-simple-cache
Advanced tools
Weekly downloads
Readme
a-simple-cache
Simple in-memory cache with additional utilities.
Cache
Cache
is a low-level interface for storing values to and retrieving values from a global object.
import { Cache } from 'a-simple-cache';
// for one minute
Cache.set('key', 'value', 60000);
// for three minutes
Cache.set('my:id', {
'number': 42,
'string': 'Hello!',
}, 180000);
Cache.get('key');
'value'
Cache.get('my:id');
{ 'number': 42, 'string': 'Hello!' }
Cache.isValid('key');
true
// one minute passes
Cache.isValid('key');
false
Cache.isValid('my:id');
true
// another two minutes pass
Cache.isValid('my:id');
false
Cache.keys();
[ 'key', 'my:id' ]
// or use a filter
Cache.keys(k => k.startsWith('my:'));
[ 'my:id' ]
Cache.clear();
// or
for (const key of Cache.keys()) {
if (!Cache.isValid(key)) {
Cache.delete(key);
}
}
Cache.keys();
[ ]
time
Caching time is expressed as a number of milliseconds, but the library provides a few convenience constants.
import { time } from 'a-simple-cache';
time.second, time.minute, time.hour
1000, 60000, 3600000
time.day, time.week, time.month
86400000, 604800000, 2592000000
Memoize
Memoize
is a function wrapper that automatically caches values returned by the function for any given argument combination.
it
import { Memoize } from 'a-simple-cache';
function expensive(arg) { ... }
const memoizedExpensive = Memoize.it(expensive, time.hour);
// runs function
memoizedExpensive(0);
// gets value from cache
memoizedExpensive(0);
// one hour passes
// runs function again
memoizedExpensive(0);
invalidate
Sometimes you need to invalidate the cache prematurely so that the function will start to run again for all argument combinations.
// runs function
memoizedExpensive(1);
// gets value from cache
memoizedExpensive(1);
Memoize.invalidate(expensive);
// or
Memoize.invalidate(memoizedExpensive);
// one hour has not passed
// runs function again
memoizedExpensive(1);
npm test
npm run coverage
FAQs
security holding package
The npm package a-simple-cache receives a total of 1 weekly downloads. As such, a-simple-cache popularity was classified as not popular.
We found that a-simple-cache demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.
Company News
Come meet the Socket team at BSidesSF and RSA! We're sponsoring several fun networking events and we would love to see you there.
Security News
OSI is starting a conversation aimed at removing the excuse of the SaaS loophole for companies navigating licensing and the complexities of doing business with open source.