Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <lenon@athenna.io>",
Expand Down
9 changes: 8 additions & 1 deletion src/cache/drivers/Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Client = any> {
/**
Expand Down Expand Up @@ -37,6 +37,12 @@ export abstract class Driver<Client = any> {
*/
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.
*/
Expand All @@ -63,6 +69,7 @@ export abstract class Driver<Client = any> {
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)
Expand Down
24 changes: 24 additions & 0 deletions src/cache/drivers/MemoryDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export class MemoryDriver extends Driver<LRUCache<string, any>> {
* Reset all data defined inside cache.
*/
public async truncate() {
if (!this.enabled) {
return
}

for (const key of this.client.keys()) {
this.client.delete(key)
}
Expand All @@ -76,6 +80,10 @@ export class MemoryDriver extends Driver<LRUCache<string, any>> {
* Get a value from the cache.
*/
public async get<T = any>(key: string, defaultValue?: T): Promise<T> {
if (!this.enabled) {
return
}

const value = this.client.get(key)

if (Is.Null(value) || Is.Undefined(value)) {
Expand All @@ -89,6 +97,10 @@ export class MemoryDriver extends Driver<LRUCache<string, any>> {
* Validate if a value exists in the cache.
*/
public async has(key: string): Promise<boolean> {
if (!this.enabled) {
return false
}

const value = await this.get(key)

return !!value
Expand All @@ -98,6 +110,10 @@ export class MemoryDriver extends Driver<LRUCache<string, any>> {
* 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, {
Expand All @@ -116,6 +132,10 @@ export class MemoryDriver extends Driver<LRUCache<string, any>> {
* the same time.
*/
public async pull<T = any>(key: string) {
if (!this.enabled) {
return
}

const value = await this.get<T>(key)

await this.delete(key)
Expand All @@ -127,6 +147,10 @@ export class MemoryDriver extends Driver<LRUCache<string, any>> {
* Delete a value from the cache.
*/
public async delete(key: string) {
if (!this.enabled) {
return
}

this.client.delete(key)
}
}
24 changes: 24 additions & 0 deletions src/cache/drivers/RedisDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ export class RedisDriver extends Driver<RedisClientType> {
* Reset all data defined inside cache.
*/
public async truncate() {
if (!this.enabled) {
return
}

let cursor = '0'

do {
Expand All @@ -174,6 +178,10 @@ export class RedisDriver extends Driver<RedisClientType> {
* Get a value from the cache.
*/
public async get<T = any>(key: string, defaultValue?: T): Promise<T> {
if (!this.enabled) {
return
}

const value = await this.client.get(this.getCacheKey(key))

if (Is.Null(value) || Is.Undefined(value)) {
Expand All @@ -187,6 +195,10 @@ export class RedisDriver extends Driver<RedisClientType> {
* Validate if a value exists in the cache.
*/
public async has(key: string): Promise<boolean> {
if (!this.enabled) {
return
}

const value = await this.get(key)

return !!value
Expand All @@ -196,6 +208,10 @@ export class RedisDriver extends Driver<RedisClientType> {
* 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, {
Expand All @@ -217,6 +233,10 @@ export class RedisDriver extends Driver<RedisClientType> {
* the same time.
*/
public async pull<T = any>(key: string) {
if (!this.enabled) {
return
}

const value = await this.get<T>(key)

await this.delete(key)
Expand All @@ -228,6 +248,10 @@ export class RedisDriver extends Driver<RedisClientType> {
* Delete a value from the cache.
*/
public async delete(key: string) {
if (!this.enabled) {
return
}

await this.client.del(this.getCacheKey(key))
}
}
8 changes: 8 additions & 0 deletions src/types/StoreOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down