I have a simple nestjs application, where I have set up a CacheModule
using Redis
store as follows:
import * as redisStore from 'cache-manager-redis-store';
CacheModule.register({
store: redisStore,
host: 'redis',
port: 6379,
}),
I would like to use it to store a single value, however, I do not want to do it the built-in way by attaching an interceptor to a controller method, but instead I want to control it manually and be able to set and retrieve the value in the code.
How would I go about doing that and would I even use cache manager for that?
3
Answers
Building on Ahmad's comment above, I used the following to enable redis in my nestjs application:
Install and setup
nestjs-redis
https://www.npmjs.com/package/nestjs-redis per docs.See the docs here on how to write and read values in a Redis store: https://github.com/NodeRedis/node-redis
If you’re connection a external Redis, I recommend to use ‘async-redis’ package.
The code will be:
On redisConfig:
So, you run:
Now you can, execute commands like set and get for your Redis Database.
You can use the official way from Nest.js:
1. Create your RedisCacheModule:
1.1.
redisCache.module.ts
:1.2.
redisCache.service.ts
:2. Inject RedisCacheModule wherever you need it:
Let’s just assume we will use it in module
DailyReportModule
:2.1.
dailyReport.module.ts
:2.2.
dailyReport.service.ts
:We will use the
redisCacheService
here:You can check my sample code here.