@avanio/expire-cache

Typescript/Javascript cache interfaces and expiration cache class.
This package contains:
examples
Synchronous example:
import {ICache, ExpireCache, ExpireTimeoutCache} from '@avanio/expire-cache';
const cache = new ExpireCache<string>();
const cache = new ExpireTimeoutCache<string>();
cache.onClear((cleared) => {
for (const [key, value] of cleared.entries()) {
console.log(`key ${String(key)} expired, deleted or clear with value ${value}`);
}
});
cache.add('key', 'value', new Date(Date.now() + 1000));
cache.add('key2', 'value2');
cache.get('key');
cache.has('key');
cache.delete('key');
cache.clear();
cache.size();
function useCache(cache: ICache<string>) {
const value = cache.get('key');
}
Synchronous/Asynchronous example (works with both ICache and IAsyncCache interfaces):
import {ICacheOrAsync} from '@avanio/expire-cache';
function useCache(cache: ICacheOrAsync<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);