CacheManager 基于 node-cache-manager, 为 NocoBase 提供 Cache 模块管理功能。内置的 Cache 类型为
更多类型可通过 API 进行扩展注册。
Store: 定义一种缓存方式,包含创建缓存的工厂方法,和其他相关配置。每种缓存方式都有一个唯一标识,在注册的时候提供。
内置的两种缓存方式对应的唯一标识即 memory 和 redis.
Store 工厂方法:由 node-cache-manager 和相关扩展包提供,用于创建缓存的的方法。如 node-cache-manager 默认提供的 'memory', node-cache-manager-redis-yet 提供的 redisStore 等。即 node-cache-manager 的 caching 方法的第一个参数。
Cache: NocoBase 封装的类,提供了使用缓存的相关方法。实际使用缓存时操作的是 Cache 的实例,每个 Cache 实例有唯一的标识,可作为区分不同模块的命名空间。
constructor()constructor(options?: CacheManagerOptions)| 属性 | 类型 | 描述 | 
|---|---|---|
defaultStore | string | 默认 Cache 类型的唯一标识 | 
stores | Record<string, StoreOptions> | 注册 Cache 类型,key为 Cache 类型的唯一标识,值为包含 Cache 类型的注册方法和全局配置的对象。 在 node-cache-manager 中,创建缓存的方法为 await caching(store, config). 而在这里要提供的对象为 StoreOptions | 
| 属性 | 类型 | 描述 | 
|---|---|---|
store | memory | FactoryStore<Store, any> | store工厂方法, 对应 caching 第一个参数 | 
close | (store: Store) => Promise<void> | 可选。如果是 Redis 等需要建立连接的中间件,需要提供一个关闭连接的回调方法,入参为store工厂方法返回的对象 | 
[key: string] | any | 其他 store 全局配置,对应 caching 第二个参数 | 
optionsoptions 参数会和默认 options 合并,默认 options 参数已有内容可以缺省,例如:
registerStore()注册新的缓存方式,参考
registerStore(options: { name: string } & StoreOptions)createCache()创建缓存,参考
createCache(options: { name: string; prefix?: string; store?: string; [key: string]: any }): Promise<Cache>| 属性 | 类型 | 描述 | 
|---|---|---|
name | string | cache 唯一标识 | 
store | string | store 唯一标识 | 
prefix | string | 可选,缓存 key 前缀 | 
[key: string] | any | 其他 store 相关的自定义配置项 | 
store 省略时,将使用 defaultStore , 此时缓存方式会跟随系统默认缓存方式改变而改变。
没有自定义配置时,会返回由全局配置创建,当前缓存方式共享的默认缓存空间,推荐加上 prefix 避免 key 冲突。
参考 Cache
getCache()获取对应的缓存
getCache(name: string): CacheflushAll()重置所有缓存
close()关闭所有缓存中间件连接