Skip to content

Commit

Permalink
Merge branch 'docs/update-configuration-cache-with-redis' of https://…
Browse files Browse the repository at this point in the history
…github.com/Tony133/docs.nestjs.com into Tony133-docs/update-configuration-cache-with-redis
  • Loading branch information
kamilmysliwiec committed Oct 18, 2024
2 parents ec16fd2 + 89a2610 commit a3e5e31
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions content/techniques/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,32 +229,38 @@ class HttpCacheInterceptor extends CacheInterceptor {
#### Different stores
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.
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, use the `registerAsync()` method to initialize the store, as follows:
```typescript
import type { RedisClientOptions } from 'redis';
import * as redisStore from 'cache-manager-redis-store';
import { redisStore } from 'cache-manager-redis-yet';
import { Module } from '@nestjs/common';
import { CacheModule } from '@nestjs/cache-manager';
import { CacheModule, CacheStore } from '@nestjs/cache-manager';
import { AppController } from './app.controller';

@Module({
imports: [
CacheModule.register<RedisClientOptions>({
store: redisStore,

// Store-specific configuration:
host: 'localhost',
port: 6379,
}),
CacheModule.registerAsync({
useFactory: async () => {
const store = await redisStore({
socket: {
host: 'localhost',
port: 6379,
},
});

return {
store: store as unknown as CacheStore,
ttl: 3 * 60000, // 3 minutes (milliseconds)
};
}
})
],
controllers: [AppController],
})
export class AppModule {}
```
> 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
> 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.
> warning**Warning** `cache-manager-redis-yet` requires the ttl to be passed directly rather than as module options.
#### Async configuration
Expand Down

0 comments on commit a3e5e31

Please sign in to comment.