Added test suite
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 53s

This commit is contained in:
2025-05-14 16:30:42 -04:00
parent cf122ef9e8
commit fec373ca4c
32 changed files with 1719 additions and 310 deletions

View File

@ -99,18 +99,20 @@ export class Cache<K extends string | number | symbol, T> {
/**
* Remove all keys from cache
*/
clear() {
clear(): this {
this.complete = false;
this.store = <any>{};
return this;
}
/**
* Delete an item from the cache
* @param {K} key Item's primary key
*/
delete(key: K) {
delete(key: K): this {
delete this.store[key];
this.save();
return this;
}
/**
@ -126,10 +128,11 @@ export class Cache<K extends string | number | symbol, T> {
* Manually expire a cached item
* @param {K} key Key to expire
*/
expire(key: K) {
expire(key: K): this {
this.complete = false;
if(this.options.expiryPolicy == 'keep') (<any>this.store[key])._expired = true;
else this.delete(key);
return this;
}
/**
@ -137,7 +140,7 @@ export class Cache<K extends string | number | symbol, T> {
* @param {K} key Key to lookup
* @return {T} Cached item
*/
get(key: K, expired?: boolean): T | null {
get(key: K, expired?: boolean): CachedValue<T> | null {
const cached = deepCopy<any>(this.store[key] ?? null);
if(expired || !cached?._expired) return cached;
return null;
@ -178,7 +181,7 @@ export class Cache<K extends string | number | symbol, T> {
if(ttl) setTimeout(() => {
this.expire(key);
this.save();
}, ttl * 1000);
}, (ttl || 0) * 1000);
return this;
}