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.9.0",
"version": "5.10.0",
"description": "The cache handler for Athenna Framework.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
32 changes: 31 additions & 1 deletion src/cache/drivers/RedisDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,34 @@ export class RedisDriver extends Driver<RedisClientType> {
return
}

const config = Config.get(`cache.stores.${this.store}`)
const { createClient } = this.getRedis()

this.client = createClient({ url: this.url, socket: this.socket })
const defaultReconnectStrategy = (retries: number) => {
if (retries >= 5) {
return new Error(
`Cache store "${this.store}": max reconnect retries reached`
)
}
return Math.min(retries * 200, 2000)
}

this.client = createClient({
url: this.url,
socket: {
...this.socket,
reconnectStrategy: config?.reconnectStrategy ?? defaultReconnectStrategy
}
})

// Required: without this listener node-redis emits 'error' events as
// unhandled rejections whenever the connection drops after initial connect.
this.client.on('error', err => {
Log.channelOrVanilla('application').error(
`({red} Error) on ({yellow} ${this.store}) cache store: ${err.message}`
)
})

this.client
.connect()
.then(() => {
Expand All @@ -118,6 +143,11 @@ export class RedisDriver extends Driver<RedisClientType> {
)
}
})
.catch(err => {
Log.channelOrVanilla('application').error(
`Failed to connect to ({yellow} ${this.store}) cache store: ${err.message}`
)
})

this.isConnected = true
this.isSavedOnFactory = options.saveOnFactory
Expand Down
11 changes: 11 additions & 0 deletions src/types/StoreOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,16 @@ export type StoreOptions = {
* @default Config.get(`cache.stores.${store}.maxEntrySize`)
*/
maxEntrySize?: number

/**
* Define a custom reconnect strategy for the Redis connection.
* Receives the number of retries so far. Return a number (ms delay
* before next retry) or an Error to stop retrying and reject all
* pending commands. Defaults to exponential backoff that gives up
* after 5 retries.
*
* @default built-in exponential backoff, max 5 retries
*/
reconnectStrategy?: (retries: number) => number | Error
}
}
Loading