simple-in-memory-cache
v0.4.3
Published
A simple in-memory cache, for nodejs and the browser, with time based expiration policies.
Maintainers
Readme
simple-in-memory-cache
A simple, typed, in-memory cache for nodejs and the browser with time-based expiration policies.
install
npm install --save simple-in-memory-cacheusage
set and get
import { createCache } from 'simple-in-memory-cache';
const { set, get } = createCache();
set('purpose of life', 42);
const purpose = get('purpose of life'); // returns 42expiration
items in the cache expire after 5 minutes by default.
change the default expiration on cache creation:
const { set, get } = createCache({ expiration: { minutes: 10 } });override expiration per item:
set('ice cream state', 'solid', { expiration: { seconds: 30 } });set an item to never expire:
set('speed of light', 299792458, { expiration: null });invalidation
invalidate a cached item with set(key, undefined):
set('purpose of life', 42);
get('purpose of life'); // returns 42
set('purpose of life', undefined);
get('purpose of life'); // returns undefinedkeys
list all non-expired keys in the cache:
const { set, keys } = createCache();
set('a', 1);
set('b', 2);
keys(); // returns ['a', 'b']types
the cache is fully typed:
import { createCache, SimpleInMemoryCache } from 'simple-in-memory-cache';
const cache: SimpleInMemoryCache<number> = createCache<number>();
cache.set('answer', 42);
const answer: number | undefined = cache.get('answer');api
createCache<T>(options?)
creates a new cache instance.
options:
expiration?: IsoDuration | null— default expiration for items (default:{ minutes: 5 })
returns: SimpleInMemoryCache<T>
SimpleInMemoryCache<T>
get(key: string): T | undefined— retrieve an item (returnsundefinedif absent or expired)set(key: string, value: T, options?): void— store an item (or invalidate withundefined)keys(): string[]— list all non-expired keys
