Compare commits

...

3 Commits

Author SHA1 Message Date
cf122ef9e8 * Fixed cache expiry
All checks were successful
Build / Build NPM Project (push) Successful in 46s
Build / Tag Version (push) Successful in 8s
Build / Publish Documentation (push) Successful in 34s
2025-05-13 11:13:48 -04:00
76b570b3fe * Fixed cache expire checks on uncached
All checks were successful
Build / Build NPM Project (push) Successful in 39s
Build / Tag Version (push) Successful in 8s
Build / Publish Documentation (push) Successful in 35s
2025-05-13 11:10:23 -04:00
4fecf10d11 * Fixed cache value type
All checks were successful
Build / Build NPM Project (push) Successful in 1m16s
Build / Tag Version (push) Successful in 14s
Build / Publish Documentation (push) Successful in 54s
2025-05-13 10:53:17 -04:00
2 changed files with 5 additions and 5 deletions

View File

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

View File

@ -11,7 +11,7 @@ export type CacheOptions = {
expiryPolicy?: 'delete' | 'keep';
}
export type CachedValue<T> = T | {_expired?: boolean};
export type CachedValue<T> = T & {_expired?: boolean};
/**
* Map of data which tracks whether it is a complete collection & offers optional expiry of cached values
@ -119,7 +119,7 @@ export class Cache<K extends string | number | symbol, T> {
*/
entries(expired?: boolean): [K, CachedValue<T>][] {
return deepCopy<any>(Object.entries(this.store)
.filter((v: any) => expired || !v._expired));
.filter((v: any) => expired || !v?._expired));
}
/**
@ -139,7 +139,7 @@ export class Cache<K extends string | number | symbol, T> {
*/
get(key: K, expired?: boolean): T | null {
const cached = deepCopy<any>(this.store[key] ?? null);
if(expired || !cached._expired) return cached;
if(expired || !cached?._expired) return cached;
return null;
}
@ -172,7 +172,7 @@ export class Cache<K extends string | number | symbol, T> {
* @return {this}
*/
set(key: K, value: T, ttl = this.options.ttl): this {
if(this.options.expiryPolicy == 'keep') delete (<any>this.store[key])._expired;
if(this.options.expiryPolicy == 'keep') delete (<any>value)._expired;
this.store[key] = value;
this.save();
if(ttl) setTimeout(() => {