From 2c2831f03ceed0fb34b3104965e1639d719e9c05 Mon Sep 17 00:00:00 2001 From: Helmi Pradita Date: Sat, 31 Oct 2026 16:33:45 +0700 Subject: [PATCH 1/2] docs(caching): add error event handler example for keyv redis store --- content/data/caching.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/content/data/caching.md b/content/data/caching.md index 1a6e8dfa50..6ebbc9973b 100644 --- a/content/data/caching.md +++ b/content/data/caching.md @@ -241,6 +241,9 @@ import { createKeyv } from '@keyv/redis'; import { Keyv } from 'keyv'; import { KeyvCacheableMemory } from 'cacheable'; +const redisStore = createKeyv('redis://localhost:6379'); +redisStore.on('error', (err) => console.error('Redis connection error:', err)); + @Module({ imports: [ CacheModule.registerAsync({ @@ -250,7 +253,7 @@ import { KeyvCacheableMemory } from 'cacheable'; new Keyv({ store: new KeyvCacheableMemory({ ttl: 60000, lruSize: 5000 }), }), - createKeyv('redis://localhost:6379'), + redisStore, ], }; }, @@ -261,7 +264,7 @@ import { KeyvCacheableMemory } from 'cacheable'; export class AppModule {} ``` -This example registers two stores: `CacheableMemory` and a Redis store. `CacheableMemory` is an in-memory store created with the `KeyvCacheableMemory` storage adapter, while the Redis store is created with the `createKeyv()` helper from `@keyv/redis`. The `stores` array specifies the stores you want to use. The first store in the array is the default store, and the rest are fallback stores. +This example registers two stores: `CacheableMemory` and a Redis store. `CacheableMemory` is an in-memory store created with the `KeyvCacheableMemory` storage adapter, while the Redis store is created with the `createKeyv()` helper from `@keyv/redis`. Attaching an `'error'` event listener to the Keyv Redis instance prevents unhandled error events during connection drops or reconnects. The `stores` array specifies the stores you want to use. The first store in the array is the default store, and the rest are fallback stores. See the [Keyv documentation](https://keyv.org/docs/) for more information on the available stores. From e1441c85aa8b3773627b81cb9c96754b4f933427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Sat, 31 Oct 2026 20:26:37 +0100 Subject: [PATCH 2/2] docs(caching): create the redis store inside the factory Keep createKeyv() and its error listener in useFactory, and explain in a warning that Keyv throws on an unhandled 'error' event. --- content/data/caching.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/content/data/caching.md b/content/data/caching.md index 6ebbc9973b..21f68d84c7 100644 --- a/content/data/caching.md +++ b/content/data/caching.md @@ -241,13 +241,13 @@ import { createKeyv } from '@keyv/redis'; import { Keyv } from 'keyv'; import { KeyvCacheableMemory } from 'cacheable'; -const redisStore = createKeyv('redis://localhost:6379'); -redisStore.on('error', (err) => console.error('Redis connection error:', err)); - @Module({ imports: [ CacheModule.registerAsync({ useFactory: async () => { + const redisStore = createKeyv('redis://localhost:6379'); + redisStore.on('error', (err) => console.error('Redis error:', err)); + return { stores: [ new Keyv({ @@ -264,7 +264,9 @@ redisStore.on('error', (err) => console.error('Redis connection error:', err)); export class AppModule {} ``` -This example registers two stores: `CacheableMemory` and a Redis store. `CacheableMemory` is an in-memory store created with the `KeyvCacheableMemory` storage adapter, while the Redis store is created with the `createKeyv()` helper from `@keyv/redis`. Attaching an `'error'` event listener to the Keyv Redis instance prevents unhandled error events during connection drops or reconnects. The `stores` array specifies the stores you want to use. The first store in the array is the default store, and the rest are fallback stores. +This example registers two stores: `CacheableMemory` and a Redis store. `CacheableMemory` is an in-memory store created with the `KeyvCacheableMemory` storage adapter, while the Redis store is created with the `createKeyv()` helper from `@keyv/redis`. The `stores` array specifies the stores you want to use. The first store in the array is the default store, and the rest are fallback stores. + +> warning **Warning** Always attach an `'error'` listener to the Redis store, as shown above. When the Redis connection drops, the store emits an `'error'` event, and Keyv throws if nothing listens for it, which can crash your application. See the [Keyv documentation](https://keyv.org/docs/) for more information on the available stores.