@athenna/cache
Advanced tools
+1
-1
| { | ||
| "name": "@athenna/cache", | ||
| "version": "5.9.0", | ||
| "version": "5.10.0", | ||
| "description": "The cache handler for Athenna Framework.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -53,8 +53,31 @@ /** | ||
| } | ||
| const config = Config.get(`cache.stores.${this.store}`); | ||
| const { createClient } = this.getRedis(); | ||
| this.client = createClient({ url: this.url, socket: this.socket }); | ||
| this.client.connect().then(() => { | ||
| const defaultReconnectStrategy = (retries) => { | ||
| 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(() => { | ||
| if (Config.is('rc.bootLogs', true)) { | ||
| Log.channelOrVanilla('application').success(`Successfully connected to ({yellow} ${this.store}) cache store`); | ||
| } | ||
| }) | ||
| .catch(err => { | ||
| Log.channelOrVanilla('application').error(`Failed to connect to ({yellow} ${this.store}) cache store: ${err.message}`); | ||
| }); | ||
@@ -61,0 +84,0 @@ this.isConnected = true; |
@@ -76,3 +76,13 @@ /** | ||
| 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; | ||
| }; | ||
| }; |
55678
2.79%1599
2.11%