From 8aa21017aa56b7131ff4b73528241a9c7b9e64e8 Mon Sep 17 00:00:00 2001 From: Tony133 Date: Fri, 14 Apr 2023 22:56:36 +0200 Subject: [PATCH 1/4] docs(caching): updating the cache configuration with redis --- content/techniques/caching.md | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/content/techniques/caching.md b/content/techniques/caching.md index 18e1761c1f..47fa269369 100644 --- a/content/techniques/caching.md +++ b/content/techniques/caching.md @@ -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, requires `registerAsync()` , since the store factory is initializing the connection to redis and only then returning the instance: ```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({ - 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 + }; + } + }) ], 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 From 5042fc5ef837b37b55cd6f7913aa8e1ef578419e Mon Sep 17 00:00:00 2001 From: Antonio Tripodi Date: Mon, 17 Apr 2023 10:41:26 +0200 Subject: [PATCH 2/4] Update content/techniques/caching.md Co-authored-by: Kamil Mysliwiec --- content/techniques/caching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/techniques/caching.md b/content/techniques/caching.md index 47fa269369..bb16100cd2 100644 --- a/content/techniques/caching.md +++ b/content/techniques/caching.md @@ -223,7 +223,7 @@ 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/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: +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'; From c562ad560393af53ece9d135de1afe1ca692818d Mon Sep 17 00:00:00 2001 From: Antonio Tripodi Date: Mon, 24 Apr 2023 10:48:28 +0200 Subject: [PATCH 3/4] Update content/techniques/caching.md --- content/techniques/caching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/techniques/caching.md b/content/techniques/caching.md index bb16100cd2..17d3169617 100644 --- a/content/techniques/caching.md +++ b/content/techniques/caching.md @@ -245,7 +245,7 @@ import { AppController } from './app.controller'; return { store: store as unknown as CacheStore, - ttl: 60 * 60 * 24 * 7, // 1 week + ttl: 3 * 60000, // 3 minutes (milliseconds) }; } }) From 89a2610f52ad8dd2aa855c9dc62ed1b70726ddce Mon Sep 17 00:00:00 2001 From: Tony133 Date: Mon, 24 Jul 2023 14:59:39 +0200 Subject: [PATCH 4/4] docs(): updated --- content/techniques/caching.md | 1 - 1 file changed, 1 deletion(-) diff --git a/content/techniques/caching.md b/content/techniques/caching.md index 17d3169617..24bb06ff51 100644 --- a/content/techniques/caching.md +++ b/content/techniques/caching.md @@ -226,7 +226,6 @@ class HttpCacheInterceptor extends CacheInterceptor { 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 { redisStore } from 'cache-manager-redis-yet'; import { Module } from '@nestjs/common'; import { CacheModule, CacheStore } from '@nestjs/cache-manager';