From 90571d802f691a56cf4bc6fcb23f76d4ae6f5b76 Mon Sep 17 00:00:00 2001 From: bwees Date: Fri, 30 Aug 2024 11:26:01 -0500 Subject: [PATCH 1/2] add ability to use connectors with the cache defaults to the original memory cache but the memory cache is broken into its own class --- src/connectors/memory_connector.ts | 25 ++++++++++++++++++ src/index.ts | 41 ++++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 src/connectors/memory_connector.ts diff --git a/src/connectors/memory_connector.ts b/src/connectors/memory_connector.ts new file mode 100644 index 0000000..1fcd653 --- /dev/null +++ b/src/connectors/memory_connector.ts @@ -0,0 +1,25 @@ +import { Cacheable, CacheConnector } from '..' + +export default class MemoryConnector implements CacheConnector { + #cacheables: Record> = {} + + get(key: string): Cacheable | undefined { + return this.#cacheables[key] + } + + set(key: string, data: Cacheable): void { + this.#cacheables[key] = data + } + + delete(key: string): void { + delete this.#cacheables[key] + } + + clear(): void { + this.#cacheables = {} + } + + keys(): string[] { + return Object.keys(this.#cacheables) + } +} diff --git a/src/index.ts b/src/index.ts index 97861f7..95df770 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ +import MemoryConnector from "./connectors/memory_connector" + //region Types export type CacheOptions = { /** @@ -12,6 +14,10 @@ export type CacheOptions = { * Enable/disable timings */ logTiming?: boolean + /** + * Enable/disable timings + */ + cacheConnector?: CacheConnector } type CacheOnlyCachePolicy = { @@ -48,23 +54,36 @@ export type CacheableOptions = //endregion +//region CacheConnector +/** + * Provides an interface to create custom cache connectors + */ +export interface CacheConnector { + get(key: string): Cacheable | undefined + set(key: string, data: Cacheable): void + delete(key: string): void + clear(): void + keys(): string[] +} + //region Cacheables /** - * Provides a simple in-memory cache with automatic or manual invalidation. + * Provides a cache with automatic or manual invalidation use the provided + * connector in CacheOptions, otherwise defaults to an in-memeory cache */ export class Cacheables { enabled: boolean log: boolean logTiming: boolean + connector: CacheConnector constructor(options?: CacheOptions) { this.enabled = options?.enabled ?? true this.log = options?.log ?? false this.logTiming = options?.logTiming ?? false + this.connector = options?.cacheConnector ?? new MemoryConnector() } - #cacheables: Record> = {} - /** * Builds a key with the provided strings or numbers. * @param args @@ -78,28 +97,28 @@ export class Cacheables { * @param key */ delete(key: string): void { - delete this.#cacheables[key] + this.connector.delete(key) } /** * Clears the cache by deleting all cacheables. */ clear(): void { - this.#cacheables = {} + this.connector.clear() } /** * Returns whether a cacheable is present and valid (i.e., did not time out). */ isCached(key: string): boolean { - return !!this.#cacheables[key] + return !!this.connector.get(key) } /** * Returns all the cache keys */ keys(): string[] { - return Object.keys(this.#cacheables) + return this.connector.keys() } /** @@ -139,7 +158,7 @@ export class Cacheables { const result = await this.#cacheable(resource, key, options) if (logTiming) Logger.logTimeEnd(logId) - if (log) Logger.logStats(key, this.#cacheables[key]) + if (log) Logger.logStats(key, this.connector.get(key)) return result } @@ -149,11 +168,11 @@ export class Cacheables { key: string, options?: CacheableOptions, ): Promise { - let cacheable = this.#cacheables[key] as Cacheable | undefined + let cacheable = this.connector.get(key) as Cacheable | undefined if (!cacheable) { cacheable = new Cacheable() - this.#cacheables[key] = cacheable + this.connector.set(key, cacheable) } return cacheable.touch(resource, options) @@ -166,7 +185,7 @@ export class Cacheables { * Helper class, can only be instantiated by calling its static * function `create`. */ -class Cacheable { +export class Cacheable { hits = 0 #lastFetch = 0 #initialized = false From 26e48ac6089669ccc31b43d4d3aca7ca65263382 Mon Sep 17 00:00:00 2001 From: Brandon Wees Date: Sat, 4 Jan 2025 22:46:13 -0500 Subject: [PATCH 2/2] Update index.ts --- src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 95df770..fcfd098 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,7 +15,8 @@ export type CacheOptions = { */ logTiming?: boolean /** - * Enable/disable timings + * Provides an object that conforms to + * CacheConnector to implement custom cache backends */ cacheConnector?: CacheConnector }