Updated cache
This commit is contained in:
parent
0909c4f648
commit
811d797e1b
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.16.4",
|
"version": "0.16.5",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
14
src/cache.ts
14
src/cache.ts
@ -13,7 +13,19 @@ export class Cache<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) {
|
||||||
|
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 {
|
||||||
return <K>value[this.key];
|
return <K>value[this.key];
|
||||||
|
Loading…
Reference in New Issue
Block a user