From f812e06d174c6325e83397d96eeaf73859d3d7f7 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Sun, 4 Apr 2021 11:15:41 +0530 Subject: [PATCH] more --- src/cache/adapter.ts | 50 -------------------------------------------- src/cache/default.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++ src/cache/mod.ts | 4 ++++ src/cache/redis.ts | 1 + 4 files changed, 55 insertions(+), 50 deletions(-) create mode 100644 src/cache/default.ts create mode 100644 src/cache/mod.ts diff --git a/src/cache/adapter.ts b/src/cache/adapter.ts index a04c149..a29bd89 100644 --- a/src/cache/adapter.ts +++ b/src/cache/adapter.ts @@ -1,5 +1,3 @@ -import { Collection } from '../utils/collection.ts' - /** * ICacheAdapter is the interface to be implemented by Cache Adapters for them to be usable with Harmony. * @@ -22,51 +20,3 @@ export interface ICacheAdapter { /** Entirely deletes a Cache */ deleteCache: (cacheName: string) => any } - -/** Default Cache Adapter for in-memory caching. */ -export class DefaultCacheAdapter implements ICacheAdapter { - data: { - [name: string]: Collection - } = {} - - async get(cacheName: string, key: string): Promise { - const cache = this.data[cacheName] - if (cache === undefined) return - return cache.get(key) - } - - async set( - cacheName: string, - key: string, - value: any, - expire?: number - ): Promise { - let cache = this.data[cacheName] - if (cache === undefined) { - this.data[cacheName] = new Collection() - cache = this.data[cacheName] - } - cache.set(key, value) - if (expire !== undefined) - setTimeout(() => { - cache.delete(key) - }, expire) - } - - async delete(cacheName: string, key: string): Promise { - const cache = this.data[cacheName] - if (cache === undefined) return false - return cache.delete(key) - } - - async array(cacheName: string): Promise { - const cache = this.data[cacheName] - if (cache === undefined) return - return cache.array() - } - - async deleteCache(cacheName: string): Promise { - // eslint-disable-next-line @typescript-eslint/no-dynamic-delete - return delete this.data[cacheName] - } -} diff --git a/src/cache/default.ts b/src/cache/default.ts new file mode 100644 index 0000000..401c1c2 --- /dev/null +++ b/src/cache/default.ts @@ -0,0 +1,50 @@ +import { Collection } from '../utils/collection.ts' +import type { ICacheAdapter } from './adapter.ts' + +/** Default Cache Adapter for in-memory caching. */ +export class DefaultCacheAdapter implements ICacheAdapter { + data: { + [name: string]: Collection + } = {} + + async get(cacheName: string, key: string): Promise { + const cache = this.data[cacheName] + if (cache === undefined) return + return cache.get(key) + } + + async set( + cacheName: string, + key: string, + value: any, + expire?: number + ): Promise { + let cache = this.data[cacheName] + if (cache === undefined) { + this.data[cacheName] = new Collection() + cache = this.data[cacheName] + } + cache.set(key, value) + if (expire !== undefined) + setTimeout(() => { + cache.delete(key) + }, expire) + } + + async delete(cacheName: string, key: string): Promise { + const cache = this.data[cacheName] + if (cache === undefined) return false + return cache.delete(key) + } + + async array(cacheName: string): Promise { + const cache = this.data[cacheName] + if (cache === undefined) return + return cache.array() + } + + async deleteCache(cacheName: string): Promise { + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete + return delete this.data[cacheName] + } +} diff --git a/src/cache/mod.ts b/src/cache/mod.ts new file mode 100644 index 0000000..cd30b7b --- /dev/null +++ b/src/cache/mod.ts @@ -0,0 +1,4 @@ +export * from './adapter.ts' +export * from './default.ts' +// Not exported by default +// export * from './redis.ts' diff --git a/src/cache/redis.ts b/src/cache/redis.ts index 7ce9182..8184f82 100644 --- a/src/cache/redis.ts +++ b/src/cache/redis.ts @@ -1,4 +1,5 @@ import { ICacheAdapter } from './adapter.ts' +// Not in deps.ts to allow optional dep loading import { connect, Redis,