Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(caching): updating the cache configuration with redis #2703

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions content/techniques/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,32 +223,39 @@ 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';
Tony133 marked this conversation as resolved.
Show resolved Hide resolved
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: 60 * 60 * 24 * 7, // 1 week
Tony133 marked this conversation as resolved.
Show resolved Hide resolved
};
}
})
],
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