Skip to content

Commit 8aa2101

Browse files
committed
docs(caching): updating the cache configuration with redis
1 parent 901cb40 commit 8aa2101

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

content/techniques/caching.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,32 +223,39 @@ class HttpCacheInterceptor extends CacheInterceptor {
223223
224224
#### Different stores
225225
226-
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.
226+
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/node-cache-manager/node-cache-manager-redis-yet) official package for node-cache-manager. 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, requires `registerAsync()` , since the store factory is initializing the connection to redis and only then returning the instance:
227227
228228
```typescript
229229
import type { RedisClientOptions } from 'redis';
230-
import * as redisStore from 'cache-manager-redis-store';
230+
import { redisStore } from 'cache-manager-redis-yet';
231231
import { Module } from '@nestjs/common';
232-
import { CacheModule } from '@nestjs/cache-manager';
232+
import { CacheModule, CacheStore } from '@nestjs/cache-manager';
233233
import { AppController } from './app.controller';
234234

235235
@Module({
236236
imports: [
237-
CacheModule.register<RedisClientOptions>({
238-
store: redisStore,
239-
240-
// Store-specific configuration:
241-
host: 'localhost',
242-
port: 6379,
243-
}),
237+
CacheModule.registerAsync({
238+
useFactory: async () => {
239+
const store = await redisStore({
240+
socket: {
241+
host: 'localhost',
242+
port: 6379,
243+
},
244+
});
245+
246+
return {
247+
store: store as unknown as CacheStore,
248+
ttl: 60 * 60 * 24 * 7, // 1 week
249+
};
250+
}
251+
})
244252
],
245253
controllers: [AppController],
246254
})
247255
export class AppModule {}
248256
```
249257
250-
> 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
251-
> 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.
258+
> warning**Warning** `cache-manager-redis-yet` requires the ttl to be passed directly rather than as module options.
252259
253260
#### Async configuration
254261

0 commit comments

Comments
 (0)