From 811d797e1bccbd949a2e197212f1e1e5779eefff Mon Sep 17 00:00:00 2001 From: ztimson Date: Sat, 28 Sep 2024 10:45:03 -0400 Subject: [PATCH] Updated cache --- package.json | 2 +- src/cache.ts | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 90fd910..b3a0d6e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.16.4", + "version": "0.16.5", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/cache.ts b/src/cache.ts index 97f7d93..f162f16 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -13,7 +13,19 @@ export class 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) { } + constructor(public readonly key: keyof T, public ttl?: number) { + return new Proxy(this, { + get: (target: this, prop: string | symbol) => { + if(prop in target) return (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 value[this.key];