@avanio/expire-cache

Typescript/Javascript Cache interfaces and expiration cache class.
This package contains:
- ICache common cache interface
- IAsyncCache common async cache interface
- TAnyCache type for both common cache interfaces
- ExpireCache class which implements ICache interface with value expiration
examples
Synchronous example
import {ICache, ExpireCache} from '@avanio/expire-cache';
const cache = new ExpireCache<string>();
cache.add('key', 'value', new Date(Date.now() + 1000));
cache.add('key2', 'value2');
cache.get('key');
cache.delete('key');
cache.clear();
function useCache(cache: ICache<string>) {
const value = cache.get('key');
}
Synchronous/Asynchronous example (works with both ICache and IAsyncCache interfaces)
import {TAnyCache} from '@avanio/expire-cache';
function useCache(cache: TAnyCache<string>) {
const value = await cache.get('key');
}
const cache = new ExpireCache<string>(console, {
get: LogLevel.Info,
set: LogLevel.Info,
});
(optional) default expiration in milliseconds if not specified in add() method. (if both are undefined, cache entry never expires)
const cache = new ExpireCache<string>(console, undefined, 60 * 1000);