Added cache find function
This commit is contained in:
parent
4ed23e1502
commit
11cfc67650
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.25.12",
|
||||
"version": "0.25.13",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
12
src/cache.ts
12
src/cache.ts
@ -1,5 +1,5 @@
|
||||
import {Table} from './database.ts';
|
||||
import {deepCopy, JSONSanitize} from './objects.ts';
|
||||
import {deepCopy, includes, JSONSanitize} from './objects.ts';
|
||||
|
||||
export type CacheOptions = {
|
||||
/** Delete keys automatically after x amount of seconds */
|
||||
@ -143,6 +143,16 @@ export class Cache<K extends string | number | symbol, T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the first cached item to match a filter
|
||||
* @param {Partial<T>} filter Partial item to match
|
||||
* @param {Boolean} expired Include expired items, defaults to false
|
||||
* @returns {T | undefined} Cached item or undefined if nothing matched
|
||||
*/
|
||||
find(filter: Partial<T>, expired?: boolean): T | undefined {
|
||||
return <T>Object.values(this.store).find((row: any) => (expired || !row._expired) && includes(row, filter));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get item from the cache
|
||||
* @param {K} key Key to lookup
|
||||
|
Loading…
x
Reference in New Issue
Block a user