LMi*_*ato 4 node-redis cachemanager nestjs
我目前正在使用 NestJS 的缓存管理器模块,我想知道为什么我无法像这样获得 NodeRedis 客户端:
constructor(
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {
cacheManager.store.getClient();
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
ERROR in [...].controller.ts:24:24
TS2339: Property 'getClient' does not exist on type 'Store'.
22 | @Inject(CACHE_MANAGER) private cacheManager: Cache,
23 | ) {
> 24 | cacheManager.store.getClient();
| ^^^^^^^^^
25 | }
Run Code Online (Sandbox Code Playgroud)
当我注册 CacheModule 时,我确实配置了cache-manager-redis-store,然后我想我可以获得客户端。
cache-manager-redis-storetl;dr TypeScript似乎没有正确支持,因为该RedisCache类型是私有的并且无法导入。
作为解决方法,您可以将私有类型复制到您自己的文件中:
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
import { Store } from 'cache-manager';
import Redis from 'redis';
interface RedisCache extends Cache {
store: RedisStore;
}
interface RedisStore extends Store {
name: 'redis';
getClient: () => Redis.RedisClient;
isCacheableValue: (value: any) => boolean;
}
@Injectable()
export class CacheService {
constructor(
@Inject(CACHE_MANAGER)
private cacheManager: RedisCache,
) {
cacheManager.store.getClient();
}
}Run Code Online (Sandbox Code Playgroud)
看起来CACHE_MANAGERNestJS 提供的内容是由createCacheManager创建的,它导入 npm 包cache-manager,然后caching使用您提供的 store 包调用其函数。
我认为Cache您使用的类型是从 导入的cache-manager。该类型在此处定义,并包含Store在同一文件中。getClient不是该接口上的方法方法,因此错误消息是正确的。
但是,由于您在商店中使用外部包,因此它的内容超出了您的caching-manager了解。查看 的类型cache-manager-redis-store,您可以看到有一个RedisStore类型扩展Store并包含getClient。
cacheManager从技术上来说getClient,自从您使用 redis 存储包对其进行配置以来,就已经如此,但是您需要将变量的类型设置为cacheManagerTypeScriptRedisCache才能允许它。
根据 的 DefinelyTyped 类型,如果您将包导入为 ,cache-manager-redis-store您的类型似乎应该是,但似乎存在问题,因为该命名空间未导出。redisStore.CacheManagerRedisStore.RedisCacheredisStoreCacheManagerRedisStore
存储库上有一个关于同样问题的问题,并且有一个要求 TypeScript 支持的问题。TypeScript 目前似乎还没有正确支持这个包。
| 归档时间: |
|
| 查看次数: |
6170 次 |
| 最近记录: |