0.6.2 • Published 8 months ago
@avanio/expire-cache v0.6.2
@avanio/expire-cache
Typescript/Javascript cache interfaces and expiration cache class.
This package contains:
- ICache a common cache interface
- IAsyncCache a common async cache interface
- ICacheOrAsync a type for both cache interfaces
- ExpireCache a class which implements the ICache interface with value expiration
examples
Synchronous example:
import {ICache, ExpireCache, ExpireTimeoutCache} from '@avanio/expire-cache';
const cache = new ExpireCache<string>(); // expiration on read operations
const cache = new ExpireTimeoutCache<string>(); // expiration with setTimeout
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)); // expires in 1000ms
cache.add('key2', 'value2'); // never expires (if no default expiration is set)
cache.get('key'); // 'value'
cache.has('key'); // true
cache.delete('key');
cache.clear();
cache.size(); // 1
function useCache(cache: ICache<string>) {
const value = cache.get('key'); // 'value'
}
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'); // 'value'
}
Advanced logging example, see default log mapping
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); // sets default 60 seconds expiration for add() method
0.6.2
8 months ago
0.6.1
8 months ago
0.6.0
8 months ago
0.5.1
9 months ago
0.5.0
10 months ago
0.4.0
11 months ago
0.3.3
1 year ago
0.3.0
2 years ago
0.3.2
2 years ago
0.3.1
2 years ago
0.2.0
2 years ago
0.1.0
2 years ago
0.0.3
2 years ago
0.0.5
2 years ago
0.0.4
2 years ago
0.0.7
2 years ago
0.0.6
2 years ago
0.0.2
2 years ago
0.0.1
2 years ago