-
Notifications
You must be signed in to change notification settings - Fork 59
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
Incompatible with NestJS cache store #53
Comments
The same issue |
Same Issue |
Same issue |
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. |
Nah thanks imma stay on the old version until something proper gets released |
@dougrocha You are effectively bypassing the Typescript type checks, which I would not recommend I'm just stepping back into the Typescript and Node world again after a few years working in Go and Python, but I believe my #54 ⬆️ should get things working in NestJS by narrowing the type definitions provided by this package. If @dabroek approves of the changes, then you should be unblocked. |
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..
|
My solution above is a bit messy, first I had to use "as any" to bypass type issue but should not all providers support milliseconds? Otherwise you need to add some logic as my reply above ^^^ |
I 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. |
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 |
The problem is redisStore is of type Store, and CacheStore is required. My solution is casting redisStore as CacheStore to fix it.
|
Hello i solve this problem with down grade version of some packages, go check here |
Referring to this comment, I think this issue can be solved if you use node-cache-manager-redis-yet instead. import { CacheModule, Module } from '@nestjs/common';
import { redisStore } from 'cache-manager-redis-yet';
@Module({
imports: [
CacheModule.register({
store: redisStore,
url: 'redis://localhost:6379',
}),
],
}) |
Thank you king |
Thanks |
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 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. |
The solution mentioned by @cgat is working. If we use |
Thanks It works. |
My solution with a dummy store fallback
|
Can't take credit, but chiming in, this solution worked for me in app.module. import { redisStore } from 'cache-manager-redis-store';
...
CacheModule.registerAsync < any > ({
isGlobal: true,
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => {
const store = await redisStore({
url: configService.get('REDIS_URL'),
ttl: 60,
});
return {
store: () => store,
};
},
inject: [ConfigService],
}), 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 |
ProblemExample from official docs works only for
SolutionThe node-cache-manager have now official redis/ioredis cache stores.
|
Thanks @MaksimKiselev |
@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. 🍻 |
this correct solution:
|
My solution for cache-manager v5 + cache-manager-redis-yet: |
Here is my solution. Node v:16.18.0 npm v:8.19.2
import { Body, Controller, Get, Post, Param, ParseIntPipe, Put, Delete, UseGuards, UseInterceptors, Inject } from '@nestjs/common'; @controller()
} |
|
cache.module.tsI personally don't like having my configs directly in my App Module. I separate my configs into different modules and import them into You'd also notice that I prefer to read my environment variables from the import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { CacheModule, CacheModuleAsyncOptions } from '@nestjs/cache-manager';
import { redisStore } from 'cache-manager-redis-store';
@Module({
imports: [
CacheModule.registerAsync({
isGlobal: true,
imports: [ConfigModule], // No need to call ConfigModule.forRoot again
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const isActive = ['true', '1', 'yes'].includes(
configService.get<string>('API_REDIS_STORE_IS_ACTIVE'),
);
return {
store:
isActive &&
(await redisStore({
// Store-specific configuration:
socket: {
host: configService.get<string>('API_REDIS_HOST'),
port: +configService.get<number>('API_REDIS_PORT'),
},
})), // use redis when available or default to cache store
ttl: 5000, // milliseconds
max: 10, // maximum number of items in cache
} as CacheModuleAsyncOptions;
},
}),
],
})
export class CacheConfigModule {} |
none of this works for me, I'm trying to get information from redis but I. don't get anything from the Redis database |
here is my solution CacheModule.registerAsync<RedisClientOptions>({
isGlobal: true,
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const redis = configService.get('redis');
return {
store: redisStore,
host: redis.host,
port: redis.port,
} as StoreConfig;
},
}), |
It looks like this package is not maintained any more, and, although I'm facing the same issue at the moment, I moved to this other (which is a fork from this one but actively maintained): https://github.com/node-cache-manager/node-cache-manager-redis-yet Also, created this issue to be properly tracked: https://github.com/node-cache-manager/node-cache-manager-redis-yet/issues/394 |
I found a thing , when i save key value with TTL in redis from nestjs server after ttl expired, the key value not deleting. @module({
], import { Controller, Get, Inject } from '@nestjs/common'; @controller() @get('health-check')
} |
I am currently facing this issue, seems it is not using my redis but rather memory. |
I found solution
|
I found a way to fix this issue, casting redisStore to 'any' to bypass the TypeScript error:
|
import { Module } from '@nestjs/common';
import { CacheModule } from '@nestjs/cache-manager';
import { redisStore } from 'cache-manager-redis-yet';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [
CacheModule.registerAsync({
isGlobal: true,
useFactory: async () => ({
store: await redisStore({
socket: {
host: 'localhost',
port: 6379
}
})
})
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { } |
None of above solution works for me. It seems to work because in-memory cache is used silently instead of Redis. Try turning off Redis (image or service) and try to check if it even connects to it. Working solution as of Nov 2024 with getting values from imports: [
CacheModule.registerAsync({
isGlobal: true,
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService): Promise<CacheModuleOptions> => {
try {
const host = configService.get<string>('REDIS_HOST');
const port = configService.get<number>('REDIS_PORT');
const store = await redisStore({
url: `redis://${host}:${port}`,
});
return { store };
} catch (error) {
console.error(error);
}
},
}),
], |
V3.0.1 is incompatible with NestJS . Unable to register CacheModule using the RedisStore.
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 {}
`
The text was updated successfully, but these errors were encountered: