Updated cache
All checks were successful
Build / Build NPM Project (push) Successful in 25s
Build / Tag Version (push) Successful in 6s

This commit is contained in:
Zakary Timson 2024-09-28 10:30:20 -04:00
parent 8384d6a299
commit 0909c4f648
2 changed files with 42 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.16.3", "version": "0.16.4",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@ -1,7 +1,8 @@
/** /**
* Map of data which tracks whether it is a complete collection & offers optional expiry of cached values * 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> { export class Cache<K, T> {
private store: any = {};
/** Whether cache is complete */ /** Whether cache is complete */
complete = false; complete = false;
@ -12,9 +13,7 @@ export class Cache<K, T> extends Map<K, T> {
* @param {keyof T} key Default property to use as primary key * @param {keyof T} key Default property to use as primary key
* @param {number} ttl Default expiry in milliseconds * @param {number} ttl Default expiry in milliseconds
*/ */
constructor(public readonly key: keyof T, public ttl?: number) { constructor(public readonly key: keyof T, public ttl?: number) { }
super();
}
private getKey(value: T): K { private getKey(value: T): K {
return <K>value[this.key]; return <K>value[this.key];
@ -25,8 +24,8 @@ export class Cache<K, T> extends Map<K, T> {
* *
* @return {T[]} Array of items * @return {T[]} Array of items
*/ */
all() { all(): T[] {
return Array.from(this.values()); return Object.values(this.store);
} }
/** /**
@ -55,19 +54,53 @@ export class Cache<K, T> extends Map<K, T> {
return this; return this;
} }
/**
* Delete an item from the cache
*
* @param {K} key Item's primary key
*/
delete(key: K) {
delete this.store[key];
}
/**
* Return cache as an array of key-value pairs
* @return {[K, T][]} Key-value pairs array
*/
entries(): [K, T][] {
return <[K, T][]>Object.entries(this.store);
}
/**
* Get a list of cached keys
*
* @return {K[]} Array of keys
*/
keys(): K[] {
return <K[]>Object.keys(this.store);
}
/** /**
* Add an item to the cache manually specifying the key * Add an item to the cache manually specifying the key
*
* @param {K} key Key item will be cached under * @param {K} key Key item will be cached under
* @param {T} value Item to cache * @param {T} value Item to cache
* @param {number | undefined} ttl Override default expiry * @param {number | undefined} ttl Override default expiry
* @return {this} * @return {this}
*/ */
set(key: K, value: T, ttl = this.ttl): this { set(key: K, value: T, ttl = this.ttl): this {
super.set(key, value); this.store[key] = value;
if(ttl) setTimeout(() => { if(ttl) setTimeout(() => {
this.complete = false; this.complete = false;
super.delete(key) this.delete(key);
}, ttl); }, ttl);
return this; return this;
} }
/**
* Get all cached items
*
* @return {T[]} Array of items
*/
values = this.all();
} }