node-cache-manager-redis-store: Incompatible with NestJS cache store
V3.0.1 is incompatible with NestJS . Unable to register CacheModule using the RedisStore.
"message": "Type 'typeof import(\"cache-test/node_modules/cache-manager-redis-store/dist/index\")' is not assignable to type '(string | CacheStoreFactory | CacheStore) & typeof import(\"/Users/kk/dev/nodejs/cache-test/node_modules/cache-manager-redis-store/dist/index\")'.\n ...
Here is the code I am using in the module.ts : ` import * as redisStore from “cache-manager-redis-store”; import { ConfigModule } from ‘@nestjs/config’;
@Module({ imports: [ ConfigModule.forRoot(), CacheModule.register({ isGlobal: true, store: redisStore, url: “redis://localhost:6379”, }), HttpModule, ], controllers: [AppController], providers: [AppService], }) export class AppModule {} `
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 33
- Comments: 35
Hello i solve this problem with down grade version of some packages, go check here https://github.com/Pangeran29/testing-nestjs/blob/master/src/testing-cache-redis/testing-cahce-redis.MD
Problem
Example from official docs works only for
cache-manager-redis-store@^2.0.0cache-manager-redis-store@^2.0.0incorrectly implement CacheManager interface and accepts TTL as object where ttl property should be in seconds{ ttl: TTL_IN_SECONDS }see: https://github.com/dabroek/node-cache-manager-redis-store/issues/53#issuecomment-1325108666Solution
The node-cache-manager have now official redis/ioredis cache stores.
npm i --save cache-manager-redis-yetI managed to go through this error using require
When i used import and bypass using some weird thing in typescript i cant get anything in Redis container and was receiveng some errors in cache service. by using the code above, i managed to do things work.
const redisStore = require('cache-manager-redis-store').redisStore;cache.module.ts
I personally don’t like having my configs directly in my App Module. I separate my configs into different modules and import them into
App module.You’d also notice that I prefer to read my environment variables from the
ConfigServiceinstead of reading directly fromprocess.env(This way you have more control for where env files are being read from)Referring to this comment, I think this issue can be solved if you use node-cache-manager-redis-yet instead.
For NestJs 8, I wasn’t able to get any of the solutions to work.
One solution shown in the NestJs does work
But you need to ensure that this is using
cache-manager-redis-store2.0.The problem is redisStore is of type Store, and CacheStore is required. My solution is casting redisStore as CacheStore to fix it.
CacheModule.register({ store: redisStore as unknown as CacheStore, host: 'localhost', port: 6379, }),Also there is another issue. Here is my solution. The in memory provider supports milliseconds by default but the redis provider (this one) supports seconds.
And as others have mentioned, the 3rd argument on the standard in-memory provider takes a number but this provider needs an object with key “ttl” and a number.
More info here…
This package is riddled with issues, bad typing, missing methods that are not exposed and so on. I appreciate it for bridging nest’s cache manager and redis but at this point I think it’s easier to use something like
iorediswith very basic abstractions for the set and get methods, and just be done with it.This issue prevents me from properly closing the connection after running my end to end tests. Previously I couldn’t set redis clusters properly because of the way the client is set up and exposed through this additional layer. It works great for simpler projects anyway.
I found a way to fix this issue although it may not be nice.
I’m not exactly sure what’s going on outside of the fact the types don’t match.
here is my solution
@alexphamhp I meant that cache-manager-redis-store@^2.0.0 incorrectly implement CacheManager interface and accept ttl parameter as object with ttl property where ttl in seconds.
CacheManager interface define ttl parameter as number where ttl must be in microseconds.
I’ve update original comment. Thx. 🍻
My solution for cache-manager v5 + cache-manager-redis-yet: https://github.com/node-cache-manager/node-cache-manager/issues/210#issuecomment-1519127408
none of this works for me, I’m trying to get information from redis but I. don’t get anything from the Redis database
this correct solution:
Can’t take credit, but chiming in, this solution worked for me in app.module.
Do note, for some reason ttl changed from milliseconds to seconds with this change, despite the in-memory caching being in milliseconds with my environment
The solution mentioned by @cgat is working. If we use
cache-manager-redis-store2.0. It fixes the problemNah thanks imma stay on the old version until something proper gets released