Skip to content

Commit 3b1ddaa

Browse files
Merge branch 'Tony133-docs/update-configuration-cache-with-redis'
2 parents ec16fd2 + 62935e2 commit 3b1ddaa

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

content/techniques/caching.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -229,32 +229,38 @@ class HttpCacheInterceptor extends CacheInterceptor {
229229
230230
#### Different stores
231231
232-
This service takes advantage of [cache-manager](https://github.com/node-cache-manager/node-cache-manager) under the hood. The `cache-manager` package supports a wide-range of useful stores, for example, [Redis store](https://github.com/dabroek/node-cache-manager-redis-store). A full list of supported stores is available [here](https://github.com/node-cache-manager/node-cache-manager#store-engines). To set up the Redis store, simply pass the package together with corresponding options to the `register()` method.
232+
The `cache-manager` package offers a variety of useful storage options, including the [Redis store](https://www.npmjs.com/package/cache-manager-redis-yet), which is the official package for integrating Redis with cache-manager. You can find a comprehensive list of supported stores [here](https://github.com/jaredwray/cacheable/blob/main/packages/cache-manager/READMEv5.md#store-engines). To configure the Redis store, use the `registerAsync()` method to initialize it, as shown below:
233233
234234
```typescript
235-
import type { RedisClientOptions } from 'redis';
236-
import * as redisStore from 'cache-manager-redis-store';
235+
import { redisStore } from 'cache-manager-redis-yet';
237236
import { Module } from '@nestjs/common';
238-
import { CacheModule } from '@nestjs/cache-manager';
237+
import { CacheModule, CacheStore } from '@nestjs/cache-manager';
239238
import { AppController } from './app.controller';
240239

241240
@Module({
242241
imports: [
243-
CacheModule.register<RedisClientOptions>({
244-
store: redisStore,
245-
246-
// Store-specific configuration:
247-
host: 'localhost',
248-
port: 6379,
242+
CacheModule.registerAsync({
243+
useFactory: async () => {
244+
const store = await redisStore({
245+
socket: {
246+
host: 'localhost',
247+
port: 6379,
248+
},
249+
});
250+
251+
return {
252+
store: store as unknown as CacheStore,
253+
ttl: 3 * 60000, // 3 minutes (milliseconds)
254+
};
255+
},
249256
}),
250257
],
251258
controllers: [AppController],
252259
})
253260
export class AppModule {}
254261
```
255262
256-
> warning**Warning** `cache-manager-redis-store` does not support redis v4. In order for the `ClientOpts` interface to exist and work correctly you need to install the
257-
> latest `redis` 3.x.x major release. See this [issue](https://github.com/dabroek/node-cache-manager-redis-store/issues/40) to track the progress of this upgrade.
263+
> warning **Warning** The `cache-manager-redis-yet` package requires the `ttl` (time-to-live) setting to be specified directly rather than as part of the module options.
258264
259265
#### Async configuration
260266

0 commit comments

Comments
 (0)