node-cache-manager-redis-store
node-cache-manager-redis-store copied to clipboard
Upgrade redis to v4
To use newest types features for typescript and avoid errors with types when using latest 3.x versions
Any update on when will this be completed?
+1
+1
+1
This is in the works. Stay tuned.
Hi, has there been any progress on this?
+1
+1
+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.
Is it work now for Nest.js ?
@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 // @ts-ignore
for now, since the interface does not allow Promise<CacheStore>
to be returned.
@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
// @ts-ignore
for now, since the interface does not allowPromise<CacheStore>
to be returned.
this can't work becouse store
field is required for option passed in redisStore function
@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
// @ts-ignore
for now, since the interface does not allowPromise<CacheStore>
to be returned.this can't work becouse
store
field is required for option passed in redisStore function
Is it not why // @ts-ignore is there?
@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
// @ts-ignore
for now, since the interface does not allowPromise<CacheStore>
to be returned.this can't work becouse
store
field is required for option passed in redisStore functionIs it not why // @ts-ignore is there?
// @ts-ignore works for next line, so effected line is store: async () => await redisStore({
, but error is inside redisStore, there is another store
property sibling of socket
.
@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
"cache-manager": "^5.1.1",
"cache-manager-redis-store": "^3.0.1",
I found a workaround to use registerAsync:
import { CacheStore } from '@nestjs/common/cache/interfaces/cache-manager.interface'
CacheModule.registerAsync({
imports: [ConfigModule],
useFactory: async (config: ConfigService) =>
await redisStore({
socket: {
host: config.get('REDIS_HOST'),
port: +config.get('REDIS_PORT'),
},
}) as unknown as CacheStore,
inject: [ConfigService],
}),
Also:
"cache-manager": "^5.1.1",
"cache-manager-redis-store": "^3.0.1",
@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:
TypeError: store.get is not a function
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",
Using cache-manager '5.1.1' I was getting the following error:
TypeError: store.get is not a function
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. So, the question is still actual:
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
};
},
}),
works fine for me, just copy and use above code :)
Hi @dabroek, any news for the Type, my repo not allow ts ignore lol
Just make a PR to this repository, so you can use new typings https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/cache-manager-redis-store
I have used the example code above but it seems that Nestjs does not use Redis and use in-memory cache instead.
@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], }),
Here is my versions
"cache-manager": "^5.1.3",
"cache-manager-redis-yet": "^4.0.0",
"@nestjs/common": "^9.2.0",
"redis": "^4.4.0",
I think caching factory function detects 2 types of store
property as shown here
- an object of store with
create
function. - a function that returns new Redis store object.
So, what I have tried is to return an object CacheStoreFactory
with create
function, which returns the store
object and it work!
CacheModule.registerAsync<RedisClientOptions>({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => {
const store = await redisStore({
socket: {
host: configService.get('REDIS_HOST'),
port: configService.get('REDIS_PORT'),
},
});
return {
store: {
create: () => store,
},
};
},
inject: [ConfigService],
})
Thanks @ntchjb mine is now started to use Redis instead in-memory cache, but ttl is not working idk whether it is only me or everyone also experience the same stuff, I only managed to get it working by using the @CacheTTL() decorator