From 257b25b12641d454f393b9a40787ed79ddf20582 Mon Sep 17 00:00:00 2001 From: jlenon7 Date: Fri, 17 Oct 2025 11:53:51 -0300 Subject: [PATCH] feat(cache): add way to enable/disable cache driver --- package-lock.json | 4 ++-- package.json | 2 +- src/cache/drivers/Driver.ts | 9 ++++++++- src/cache/drivers/MemoryDriver.ts | 24 ++++++++++++++++++++++++ src/cache/drivers/RedisDriver.ts | 24 ++++++++++++++++++++++++ src/types/StoreOptions.ts | 8 ++++++++ 6 files changed, 67 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 00e373a..d65a7d8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/cache", - "version": "5.5.0", + "version": "5.6.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/cache", - "version": "5.5.0", + "version": "5.6.0", "license": "MIT", "devDependencies": { "@athenna/artisan": "^5.7.0", diff --git a/package.json b/package.json index 4ceedc9..db8b577 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/cache", - "version": "5.5.0", + "version": "5.6.0", "description": "The cache handler for Athenna Framework.", "license": "MIT", "author": "João Lenon ", diff --git a/src/cache/drivers/Driver.ts b/src/cache/drivers/Driver.ts index 7a03539..4b1a8a0 100644 --- a/src/cache/drivers/Driver.ts +++ b/src/cache/drivers/Driver.ts @@ -8,8 +8,8 @@ */ import { Config } from '@athenna/config' -import type { StoreOptions } from '#src/types' import { Module } from '@athenna/common' +import type { StoreOptions } from '#src/types' export abstract class Driver { /** @@ -37,6 +37,12 @@ export abstract class Driver { */ public ttl: number + /** + * Set if cache is enabled. Use this when you wait to avoid fetching from + * cache for testing purposes. + */ + public enabled: boolean + /** * Set the cache prefix of the driver. */ @@ -63,6 +69,7 @@ export abstract class Driver { const config = Config.get(`cache.stores.${store}`) this.ttl = options?.ttl || config.ttl + this.enabled = options?.enabled || config.enabled || true this.maxItems = options?.maxItems || config.maxItems || 1000 this.maxEntrySize = options?.maxEntrySize || config.maxEntrySize this.prefix = this.sanitizePrefix(options?.prefix || config?.prefix) diff --git a/src/cache/drivers/MemoryDriver.ts b/src/cache/drivers/MemoryDriver.ts index 9c5e9ed..007c793 100644 --- a/src/cache/drivers/MemoryDriver.ts +++ b/src/cache/drivers/MemoryDriver.ts @@ -67,6 +67,10 @@ export class MemoryDriver extends Driver> { * Reset all data defined inside cache. */ public async truncate() { + if (!this.enabled) { + return + } + for (const key of this.client.keys()) { this.client.delete(key) } @@ -76,6 +80,10 @@ export class MemoryDriver extends Driver> { * Get a value from the cache. */ public async get(key: string, defaultValue?: T): Promise { + if (!this.enabled) { + return + } + const value = this.client.get(key) if (Is.Null(value) || Is.Undefined(value)) { @@ -89,6 +97,10 @@ export class MemoryDriver extends Driver> { * Validate if a value exists in the cache. */ public async has(key: string): Promise { + if (!this.enabled) { + return false + } + const value = await this.get(key) return !!value @@ -98,6 +110,10 @@ export class MemoryDriver extends Driver> { * Set a value in the cache. */ public async set(key: string, value: any, options?: { ttl?: number }) { + if (!this.enabled) { + return + } + const driverOptions: any = {} options = Options.create(options, { @@ -116,6 +132,10 @@ export class MemoryDriver extends Driver> { * the same time. */ public async pull(key: string) { + if (!this.enabled) { + return + } + const value = await this.get(key) await this.delete(key) @@ -127,6 +147,10 @@ export class MemoryDriver extends Driver> { * Delete a value from the cache. */ public async delete(key: string) { + if (!this.enabled) { + return + } + this.client.delete(key) } } diff --git a/src/cache/drivers/RedisDriver.ts b/src/cache/drivers/RedisDriver.ts index 28af227..37f46c4 100644 --- a/src/cache/drivers/RedisDriver.ts +++ b/src/cache/drivers/RedisDriver.ts @@ -154,6 +154,10 @@ export class RedisDriver extends Driver { * Reset all data defined inside cache. */ public async truncate() { + if (!this.enabled) { + return + } + let cursor = '0' do { @@ -174,6 +178,10 @@ export class RedisDriver extends Driver { * Get a value from the cache. */ public async get(key: string, defaultValue?: T): Promise { + if (!this.enabled) { + return + } + const value = await this.client.get(this.getCacheKey(key)) if (Is.Null(value) || Is.Undefined(value)) { @@ -187,6 +195,10 @@ export class RedisDriver extends Driver { * Validate if a value exists in the cache. */ public async has(key: string): Promise { + if (!this.enabled) { + return + } + const value = await this.get(key) return !!value @@ -196,6 +208,10 @@ export class RedisDriver extends Driver { * Set a value in the cache. */ public async set(key: string, value: any, options?: { ttl?: number }) { + if (!this.enabled) { + return + } + const driverOptions: any = {} options = Options.create(options, { @@ -217,6 +233,10 @@ export class RedisDriver extends Driver { * the same time. */ public async pull(key: string) { + if (!this.enabled) { + return + } + const value = await this.get(key) await this.delete(key) @@ -228,6 +248,10 @@ export class RedisDriver extends Driver { * Delete a value from the cache. */ public async delete(key: string) { + if (!this.enabled) { + return + } + await this.client.del(this.getCacheKey(key)) } } diff --git a/src/types/StoreOptions.ts b/src/types/StoreOptions.ts index 597e11c..3ce500f 100644 --- a/src/types/StoreOptions.ts +++ b/src/types/StoreOptions.ts @@ -53,6 +53,14 @@ export type StoreOptions = { */ ttl?: number + /** + * Define if your cache will be enabled or not. Useful when you want + * to make tests. + * + * @default Config.get(`cache.stores.${store}.enabled`) + */ + enabled?: boolean + /** * Define a prefix for the store. By default, prefix * will always be used in front of your keys if it exists.