Added new cache object
This commit is contained in:
parent
19251244d2
commit
8384d6a299
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.15.6",
|
||||
"version": "0.16.3",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.15.6",
|
||||
"version": "0.16.3",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.12",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.16.2",
|
||||
"version": "0.16.3",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
73
src/cache.ts
Normal file
73
src/cache.ts
Normal file
@ -0,0 +1,73 @@
|
||||
/**
|
||||
* Map of data which tracks whether it is a complete collection & offers optional expiry of cached values
|
||||
*/
|
||||
export class Cache<K, T> extends Map<K, T> {
|
||||
|
||||
/** Whether cache is complete */
|
||||
complete = false;
|
||||
|
||||
/**
|
||||
* Create new cache
|
||||
*
|
||||
* @param {keyof T} key Default property to use as primary key
|
||||
* @param {number} ttl Default expiry in milliseconds
|
||||
*/
|
||||
constructor(public readonly key: keyof T, public ttl?: number) {
|
||||
super();
|
||||
}
|
||||
|
||||
private getKey(value: T): K {
|
||||
return <K>value[this.key];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all cached items
|
||||
*
|
||||
* @return {T[]} Array of items
|
||||
*/
|
||||
all() {
|
||||
return Array.from(this.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new item to the cache. Like set, but finds key automatically
|
||||
*
|
||||
* @param {T} value Item to add to cache
|
||||
* @param {number | undefined} ttl Override default expiry
|
||||
* @return {this}
|
||||
*/
|
||||
add(value: T, ttl = this.ttl): this {
|
||||
const key = this.getKey(value);
|
||||
this.set(key, value, ttl);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add several rows to the cache
|
||||
*
|
||||
* @param {T[]} rows Several items that will be cached using the default key
|
||||
* @param complete Mark cache as complete & reliable, defaults to true
|
||||
* @return {this}
|
||||
*/
|
||||
addAll(rows: T[], complete = true): this {
|
||||
rows.forEach(r => this.add(r));
|
||||
this.complete = complete;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an item to the cache manually specifying the key
|
||||
* @param {K} key Key item will be cached under
|
||||
* @param {T} value Item to cache
|
||||
* @param {number | undefined} ttl Override default expiry
|
||||
* @return {this}
|
||||
*/
|
||||
set(key: K, value: T, ttl = this.ttl): this {
|
||||
super.set(key, value);
|
||||
if(ttl) setTimeout(() => {
|
||||
this.complete = false;
|
||||
super.delete(key)
|
||||
}, ttl);
|
||||
return this;
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
export * from './array';
|
||||
export * from './aset';
|
||||
export * from './cache';
|
||||
export * from './csv';
|
||||
export * from './files';
|
||||
export * from './emitter';
|
||||
|
Loading…
Reference in New Issue
Block a user