Skip to content

Commit 1652931

Browse files
authored
Merge pull request #6 from AthennaIO/develop
feat(cache): add way to enable/disable cache driver
2 parents 24715bb + 257b25b commit 1652931

File tree

6 files changed

+67
-4
lines changed

6 files changed

+67
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/cache",
3-
"version": "5.5.0",
3+
"version": "5.6.0",
44
"description": "The cache handler for Athenna Framework.",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",

src/cache/drivers/Driver.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
*/
99

1010
import { Config } from '@athenna/config'
11-
import type { StoreOptions } from '#src/types'
1211
import { Module } from '@athenna/common'
12+
import type { StoreOptions } from '#src/types'
1313

1414
export abstract class Driver<Client = any> {
1515
/**
@@ -37,6 +37,12 @@ export abstract class Driver<Client = any> {
3737
*/
3838
public ttl: number
3939

40+
/**
41+
* Set if cache is enabled. Use this when you wait to avoid fetching from
42+
* cache for testing purposes.
43+
*/
44+
public enabled: boolean
45+
4046
/**
4147
* Set the cache prefix of the driver.
4248
*/
@@ -63,6 +69,7 @@ export abstract class Driver<Client = any> {
6369
const config = Config.get(`cache.stores.${store}`)
6470

6571
this.ttl = options?.ttl || config.ttl
72+
this.enabled = options?.enabled || config.enabled || true
6673
this.maxItems = options?.maxItems || config.maxItems || 1000
6774
this.maxEntrySize = options?.maxEntrySize || config.maxEntrySize
6875
this.prefix = this.sanitizePrefix(options?.prefix || config?.prefix)

src/cache/drivers/MemoryDriver.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export class MemoryDriver extends Driver<LRUCache<string, any>> {
6767
* Reset all data defined inside cache.
6868
*/
6969
public async truncate() {
70+
if (!this.enabled) {
71+
return
72+
}
73+
7074
for (const key of this.client.keys()) {
7175
this.client.delete(key)
7276
}
@@ -76,6 +80,10 @@ export class MemoryDriver extends Driver<LRUCache<string, any>> {
7680
* Get a value from the cache.
7781
*/
7882
public async get<T = any>(key: string, defaultValue?: T): Promise<T> {
83+
if (!this.enabled) {
84+
return
85+
}
86+
7987
const value = this.client.get(key)
8088

8189
if (Is.Null(value) || Is.Undefined(value)) {
@@ -89,6 +97,10 @@ export class MemoryDriver extends Driver<LRUCache<string, any>> {
8997
* Validate if a value exists in the cache.
9098
*/
9199
public async has(key: string): Promise<boolean> {
100+
if (!this.enabled) {
101+
return false
102+
}
103+
92104
const value = await this.get(key)
93105

94106
return !!value
@@ -98,6 +110,10 @@ export class MemoryDriver extends Driver<LRUCache<string, any>> {
98110
* Set a value in the cache.
99111
*/
100112
public async set(key: string, value: any, options?: { ttl?: number }) {
113+
if (!this.enabled) {
114+
return
115+
}
116+
101117
const driverOptions: any = {}
102118

103119
options = Options.create(options, {
@@ -116,6 +132,10 @@ export class MemoryDriver extends Driver<LRUCache<string, any>> {
116132
* the same time.
117133
*/
118134
public async pull<T = any>(key: string) {
135+
if (!this.enabled) {
136+
return
137+
}
138+
119139
const value = await this.get<T>(key)
120140

121141
await this.delete(key)
@@ -127,6 +147,10 @@ export class MemoryDriver extends Driver<LRUCache<string, any>> {
127147
* Delete a value from the cache.
128148
*/
129149
public async delete(key: string) {
150+
if (!this.enabled) {
151+
return
152+
}
153+
130154
this.client.delete(key)
131155
}
132156
}

src/cache/drivers/RedisDriver.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ export class RedisDriver extends Driver<RedisClientType> {
154154
* Reset all data defined inside cache.
155155
*/
156156
public async truncate() {
157+
if (!this.enabled) {
158+
return
159+
}
160+
157161
let cursor = '0'
158162

159163
do {
@@ -174,6 +178,10 @@ export class RedisDriver extends Driver<RedisClientType> {
174178
* Get a value from the cache.
175179
*/
176180
public async get<T = any>(key: string, defaultValue?: T): Promise<T> {
181+
if (!this.enabled) {
182+
return
183+
}
184+
177185
const value = await this.client.get(this.getCacheKey(key))
178186

179187
if (Is.Null(value) || Is.Undefined(value)) {
@@ -187,6 +195,10 @@ export class RedisDriver extends Driver<RedisClientType> {
187195
* Validate if a value exists in the cache.
188196
*/
189197
public async has(key: string): Promise<boolean> {
198+
if (!this.enabled) {
199+
return
200+
}
201+
190202
const value = await this.get(key)
191203

192204
return !!value
@@ -196,6 +208,10 @@ export class RedisDriver extends Driver<RedisClientType> {
196208
* Set a value in the cache.
197209
*/
198210
public async set(key: string, value: any, options?: { ttl?: number }) {
211+
if (!this.enabled) {
212+
return
213+
}
214+
199215
const driverOptions: any = {}
200216

201217
options = Options.create(options, {
@@ -217,6 +233,10 @@ export class RedisDriver extends Driver<RedisClientType> {
217233
* the same time.
218234
*/
219235
public async pull<T = any>(key: string) {
236+
if (!this.enabled) {
237+
return
238+
}
239+
220240
const value = await this.get<T>(key)
221241

222242
await this.delete(key)
@@ -228,6 +248,10 @@ export class RedisDriver extends Driver<RedisClientType> {
228248
* Delete a value from the cache.
229249
*/
230250
public async delete(key: string) {
251+
if (!this.enabled) {
252+
return
253+
}
254+
231255
await this.client.del(this.getCacheKey(key))
232256
}
233257
}

src/types/StoreOptions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ export type StoreOptions = {
5353
*/
5454
ttl?: number
5555

56+
/**
57+
* Define if your cache will be enabled or not. Useful when you want
58+
* to make tests.
59+
*
60+
* @default Config.get(`cache.stores.${store}.enabled`)
61+
*/
62+
enabled?: boolean
63+
5664
/**
5765
* Define a prefix for the store. By default, prefix
5866
* will always be used in front of your keys if it exists.

0 commit comments

Comments
 (0)