Compare commits

...

3 Commits

Author SHA1 Message Date
e6636d373b Updated cache
All checks were successful
Build / Build NPM Project (push) Successful in 26s
Build / Tag Version (push) Successful in 6s
2024-09-28 14:41:43 -04:00
811d797e1b Updated cache
All checks were successful
Build / Build NPM Project (push) Successful in 25s
Build / Tag Version (push) Successful in 6s
2024-09-28 10:45:03 -04:00
0909c4f648 Updated cache
All checks were successful
Build / Build NPM Project (push) Successful in 25s
Build / Tag Version (push) Successful in 6s
2024-09-28 10:30:20 -04:00
2 changed files with 61 additions and 7 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.16.3", "version": "0.16.6",
"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;
@ -13,7 +14,17 @@ export class Cache<K, T> extends Map<K, T> {
* @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(); return new Proxy(this, {
get: (target: this, prop: string | symbol) => {
if(prop in target) return (<any>target)[prop];
return target.store[prop];
},
set: (target: any, prop: string | symbol, value: T) => {
if(prop in target) target[prop] = value;
else target.store[prop] = value;
return true;
}
});
} }
private getKey(value: T): K { private getKey(value: T): K {
@ -25,8 +36,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 +66,62 @@ 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 item from the cache
* @param {K} key Key to lookup
* @return {T} Cached item
*/
get(key: K): T {
return this.store[key];
}
/**
* 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();
} }