node-cache-manager-redis-store
node-cache-manager-redis-store copied to clipboard
Property 'getClient' does not exist on type 'Store'
Getting this error with cache-manager with Typescript when trying to const redisClient = redisCache.store.getClient()
no update?
looking at @types/cache-manager-redis-store
the namespace that contains the store isn't exported.
I am still not able to getClient from store. Can someone suggest the workaround?
@Heet1996
@Module({
imports: [
// 连接redis
CacheModule.registerAsync({
isGlobal: true,
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const store = await redisStore({
socket: {
host: configService.get('REDIS_HOST'),
port: +configService.get('REDIS_PORT'),
},
ttl: 60 * 30,
});
return {
store: () => store,
} as unknown as CacheStore;
},
}),
],
providers: [RedisCacheService],
exports: [RedisCacheService],
})
The issue is that the usage example from the README: https://github.com/dabroek/node-cache-manager-redis-store/blob/04d04cb5e5a472a9f8eb78c59fa1f90eb6df5839/README.md#single-store) no longer works with the latest version of "node-cache-manager":
// not awaiting here is the first issue, since caching fn from node-cache-manager was updated in v5 to be async, see https://github.com/node-cache-manager/node-cache-manager#usage-examples
const redisCache = cacheManager.caching({
store: await redisStore(config),
})
// redisCache.store returns an object like: { store: ... }, so can only access getClient on redisCache.store.store
const redisClient = redisCache.store.getClient()
// throws error: TypeError: Cannot read properties of undefined (reading 'getClient')
dependencies: "cache-manager": "^5.2.3" "cache-manager-redis-store": "^3.0.1"
It's caused by a breaking change in v5 of node-cache-manager described here which makes passing in a store as is currently described in the README hit the else
case in the caching
fn: https://github.com/node-cache-manager/node-cache-manager/blob/d9f27d7fb74556f739df1487d1b622374886d675/src/caching.ts#L69, which sets the store
property on the object returned by caching
to the input { store: ... } object, hence the store.store
.
Indeed, downgrading to:
"cache-manager": "4.1.0"
"cache-manager-redis-store": "2.0.0"
as mentioned here restores the behaviour in the README. I have noticed that even using "cache-manager-redis-store": "^3.0.0" works properly (e.g. can do await redisCache.store.get('foo')
even though this was mentioned to be failing in the linked issue).
I've found that if using with "cache-manager": "^5.0.0", even though the call to caching
can be done in a way that is compatible with new API, the wrap
function stops working, so it is probably not a good idea to use.
Probably this package requires an update to be compatible with v5 (which from what I can see is not so likely https://github.com/node-cache-manager/node-cache-manager/issues/236)