Skip to content

Commit

Permalink
feat: add debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
PfisterFactor committed Mar 23, 2024
1 parent 2ebcef7 commit 59bd5df
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/cache/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import RedisCacheEngine from './engine/RedisCacheEngine'
class CacheEngine {
#engine!: ICacheEngine
#defaultTTL: number
#debug: boolean;
readonly #engines = ['memory', 'redis'] as const

constructor(cacheOptions: ICacheOptions) {
Expand All @@ -30,23 +31,40 @@ class CacheEngine {
if (cacheOptions.engine === 'memory') {
this.#engine = new MemoryCacheEngine()
}

this.#debug = (cacheOptions.debug === true) ?? false
}

async get(key: string): Promise<IData> {
return this.#engine.get(key)
const cacheEntry = await this.#engine.get(key)
if (this.#debug) {
const cacheHit = (cacheEntry != undefined) ? "HIT" : "MISS"
console.log(`[ts-cache-mongoose] GET '${key}' - ${cacheHit}`)
}
return cacheEntry
}

async set(key: string, value: Record<string, unknown> | Record<string, unknown>[], ttl: string | null): Promise<void> {
const actualTTL = ttl ? ms(ttl) : this.#defaultTTL
return this.#engine.set(key, value, actualTTL)
await this.#engine.set(key, value, actualTTL)
if (this.#debug) {
console.log(`[ts-cache-mongoose] SET '${key}' - ttl: ${actualTTL} ms`)
}

}

async del(key: string): Promise<void> {
return this.#engine.del(key)
await this.#engine.del(key)
if (this.#debug) {
console.log(`[ts-cache-mongoose] DEL '${key}'`)
}
}

async clear(): Promise<void> {
return this.#engine.clear()
await this.#engine.clear()
if (this.#debug) {
console.log(`[ts-cache-mongoose] CLEAR`)
}
}

async close(): Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/ICacheOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface ICacheOptions {
engine: 'memory' | 'redis'
engineOptions?: RedisOptions
defaultTTL?: string
debug?: boolean;
}

export default ICacheOptions

0 comments on commit 59bd5df

Please sign in to comment.