-
Notifications
You must be signed in to change notification settings - Fork 57
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
Upgrade redis to v4 #40
Comments
Any update on when will this be completed? |
+1 |
2 similar comments
+1 |
+1 |
This is in the works. Stay tuned. |
Hi, has there been any progress on this? |
+1 |
+1 |
4 similar comments
+1 |
+1 |
+1 |
+1 |
First of all, thank you all for showing your interest. It encouraged me to finally work on this again. I am happy to inform you that I have just released v3.0.0 of this package on npm which upgrades the redis dependency to redis@^4.3.1 and adds new TypeScript declarations. Please let me know if you have any feedback or further requests. |
@DmytroHaponovMD This can currently be achieved using: import { redisStore } from 'cache-manager-redis-store';
import { CacheModule, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [
CacheModule.register({
// @ts-ignore
store: async () => await redisStore({
// Store-specific configuration:
socket: {
host: 'localhost',
port: 6379,
}
})
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {} Unfortunately you are going to need the |
this can't work becouse |
Is it not why // @ts-ignore is there? |
// @ts-ignore works for next line, so effected line is |
@dabroek The DefinitelyTyped typings for this package need to be updated for ^3.0 https://www.npmjs.com/package/@types/cache-manager-redis-store -- this is the root of the error @DmytroHaponovMD is seeing, I believe |
This seems to be working for me: @Module({
imports: [
['true', '1', 'yes'].includes(<string>process.env.API_REDIS_STORE_IS_ACTIVE)
? CacheModule.register({
isGlobal: true,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
store: async () => {
return await redisStore({
// Store-specific configuration:
socket: {
host: process.env.API_REDIS_HOST,
port: +(<string>process.env.API_REDIS_PORT),
},
});
},
})
: CacheModule.register({ isGlobal: true }),
... With
|
I found a workaround to use registerAsync:
Also:
|
@assisgui same here but had to also configure the ttl: CacheModule.registerAsync({
imports: [ConfigModule],
useFactory: async (config: ConfigService) => {
const store = await redisStore({
socket: {
host: config.get('REDIS_HOST'),
port: +config.get('REDIS_PORT'),
},
password: config.get('REDIS_PASSWORD'),
});
return {
store: store as unknown as CacheStore,
ttl: 60 * 60 * 24 * 7,
};
},
inject: [ConfigService],
}), |
Any update on when will this be completed? |
Using cache-manager '5.1.1' I was getting the following error:
It worked for me after the cache-manager downgrade version to 4.1.0 @Module({
imports: [
CacheModule.registerAsync({
isGlobal: true,
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const store = await redisStore({
socket: {
host: config.get('REDIS_HOST'),
port: +config.get('REDIS_PORT'),
},
password: config.get('REDIS_PASSWORD'),
});
return {
store: store as unknown as CacheStore,
ttl: 5
};
}
})
]
})
export class RedisModule {} "@nestjs/core": "9.0.11",
"cache-manager": "4.1.0",
"cache-manager-redis-store": "3.0.1", |
In my case - it doesn't work, and I do not want to check all time is here fix or not. Any update on when will this be completed? |
CacheModule.registerAsync<RedisClientOptions>({
isGlobal: true,
imports: [SharedModule],
inject: [ApiConfigService],
useFactory: async (configService: ConfigService) => {
const store = await redisStore({
socket: {
host: config.get('REDIS_HOST'),
port: +config.get('REDIS_PORT'),
},
password: config.get('REDIS_PASSWORD'),
});
return {
store: {
create: () => store as unknown as CacheStore,
},
ttl: 60 * 60 * 24 * 7, // 1 week
};
},
}),
|
Hi @dabroek, any news for the Type, my repo not allow ts ignore lol |
This is the only solution that worked for me. |
Since the issue is still active, and there's no clear solution posted, allow me to provide a clean fix: Author of the original node-cache-manager library has forked this client and adapted the required fixes. It is compatible with latest versions of cache-manager and redis libraries. Replace this library with The rest of the comment is only relevant for NestJS users. How to use redis with NestJS cache managerInstall import { CacheModule, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { redisStore } from 'cache-manager-redis-yet';
@Module({
imports: [
CacheModule.registerAsync({
useFactory: async () => ({
store: await redisStore({ ttl: 5000 }),
}),
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {} You can extend on this to provide the required connection credentials to Notes about TTL
|
to change import { CacheModule, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { redisStore } from 'cache-manager-redis-yet';
@Module({
imports: [
CacheModule.registerAsync({
useFactory: async () => await redisStore({ ttl: 5000 }),
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {} to fix
|
For me it worked with useFactory: async () => {
return {
store: await redisStore({
// client options
}),
};
} |
Following async configuration working fine for me. CacheModule.registerAsync<RedisClientOptions>({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
store: redisStore as unknown as CacheStore,
url: `redis://${configService.get<string>('REDIS_HOST')}:${configService.get<number>('REDIS_PORT')}`,
database: configService.get<number>('REDIS_DB'),
ttl: 24 * 60 * 60, // 1 day
}),
isGlobal: true,
}) |
我想要在docker中使用,但会一直显示redis的自定义配置无效的问题,就像下面这样,无论 我是这样解决的,在 CacheModule.registerAsync<RedisClientOptions>({
isGlobal: true,
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
store: await redisStore({
ttl: 5000,
url: `redis://${configService.get<string>(
'REDIS_HOST'
)}:${configService.get<number>('REDIS_PORT')}`,
}),
}),
}), |
this works for me with the latest package versions: CacheModule.register({
store: async () =>
await redisStore({
socket: {
host: 'localhost',
port: 6379,
},
ttl: 10000,
}),
isGlobal: true,
}) |
This seems to work with latest packages, the former left the TTL to -1. Noticed cached items didn't expire and checked the database. CacheModule.registerAsync({
imports: [ConfigModule],
useFactory: async (config: ConfigService) => {
const store = await redisStore({
socket: {
host: config.get<string>('REDIS_HOST'),
port: config.get<number>('REDIS_PORT'),
},
password: config.get<string>('REDIS_PASSWORD'),
ttl: 60, // 60 seconds
});
return {
store: store as unknown as CacheStore,
};
},
inject: [ConfigService],
}),
|
This is my solution, works with latest version: import { RedisClientOptions } from 'redis';
import { redisStore } from 'cache-manager-redis-yet';
import { ConfigModule, ConfigService } from '@nestjs/config'; CacheModule.registerAsync<RedisClientOptions>({
imports: [ConfigModule],
useFactory: async (config: ConfigService) => ({
store: await redisStore({ url: config.get<string>('REDIS_URL') })
}),
inject: [ConfigService]
}), "@nestjs/common": "^9.0.11",
"cache-manager": "^4.1.0",
"cache-manager-redis-yet": "^4.1.1",
"redis": "^4.6.7", |
It's also important to note that if you'll be importing this cache module from within |
This worked for me.
|
Thank you @mengtongun for this solution. Just a side note, you've got a typo in the following line (Where cgf should be cfg):
|
This is interesting. A few things: The last commit from this project was October 16 2022. This project is forked to https://github.com/node-cache-manager/node-cache-manager-redis-yet#license It is officially maintained by node-cache-manager according to NestJS docs https://docs.nestjs.com/techniques/caching#different-stores Is this project abandoned? Here is a working example from node-cache-manager-redis-yet import { CacheModule } from '@nestjs/cache-manager';
import { RedisClientOptions } from 'redis';
import { redisStore } from 'cache-manager-redis-yet';
@Module({
imports: [
CacheModule.register<RedisClientOptions>({
store: redisStore,
socket: {
host: process.env.REDIS_HOST ?? 'localhost',
port: parseInt(process.env.REDIS_PORT ?? '6379'),
},
}),
],
})
export class TestModule {} ...
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}
...
await this.cacheManager.set('test', 'test-value');
const testVal = await this.cacheManager.get('test');
... "@nestjs/common": "^10.0.5",
"cache-manager": "^5.2.3",
"cache-manager-redis-yet": "^4.1.2",
"redis": "^4.6.7", |
This solutions works for me thanks. |
Please be cautious regarding this issue that I came across while using cache-manager-redis-yet. This issue could potentially lead to a security vulnerability within your application, especially if you are depending on the |
Maybe I can help someone.
With TTL - this is workes for me.
controller:
|
Seems to work!import { AppController } from "@/app.controller"
import { AppService } from "@/app.service"
import { CacheModule } from "@nestjs/cache-manager"
import { Module } from "@nestjs/common"
import { ConfigModule, ConfigService } from "@nestjs/config"
import { redisStore } from "cache-manager-redis-yet"
import type { RedisClientOptions } from "redis"
@Module({
imports: [
CacheModule.registerAsync<RedisClientOptions>({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
store: await redisStore({
ttl: parseInt(configService.get<string>("REDIS_TTL")),
socket: {
host: configService.get<string>("REDIS_HOST"),
port: parseInt(configService.get<string>("REDIS_PORT")),
},
}),
}),
isGlobal: true,
}),
ConfigModule.forRoot({
isGlobal: true,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {} |
same bug with me |
This worked for me so far @module({ |
Any update on when will this be completed? |
|
Using the following does not close the redis connection when CacheModule.registerAsync<RedisClientOptions>({
useFactory: async (configService: AppConfigService) => ({
store: await redisStore({
ttl: configService.cache.ttl,
socket: {
host: configService.cache.host,
port: configService.cache.port
}
})
}),
inject: [AppConfigService],
isGlobal: true
}), I had to implement the following to manually close the client: export class AppModule implements OnModuleDestroy {
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}
async onModuleDestroy() {
// @ts-expect-error as store.client is not currently exposed
await this.cacheManager.store.client.quit()
}
} |
update: double check the port you are running redis server on, I changed mine to 10001 and was trying to access it on 6379 (I was getting no error). Don't be like me. The configurations were working fine both redis-yet and redis-store 2.0.0.
can u please share the packages with versions you are using? redis, cache-manager, redis-yet or redis-store? any other related? |
thanks bro you saved me |
To use newest types features for typescript and avoid errors with types when using latest 3.x versions
The text was updated successfully, but these errors were encountered: