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:45:03 -04:00
parent 0909c4f648
commit 811d797e1b
2 changed files with 14 additions and 2 deletions

View File

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

View File

@ -13,7 +13,19 @@ export class Cache<K, T> {
* @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) { }
constructor(public readonly key: keyof T, public ttl?: number) {
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 {
return <K>value[this.key];