CacheManager
Overview
CacheManager is based on node-cache-manager and provides cache module management for NocoBase. The built-in cache types are
- memory - lru-cache provided by default by node-cache-manager
- redis - supported by node-cache-manager-redis-yet
More types can be registered and extended via the API.
Concepts
-
Store: Defines a caching method, including a factory method for creating caches and other related configurations. Each caching method has a unique identifier provided during registration. The unique identifiers for the two built-in caching methods are
memoryandredis. -
Store Factory Method: A method provided by
node-cache-managerand related extension packages for creating caches. For example,'memory'provided by default bynode-cache-manager, andredisStoreprovided bynode-cache-manager-redis-yet. This corresponds to the first parameter of thecachingmethod innode-cache-manager. -
Cache: A class encapsulated by NocoBase that provides methods for using the cache. When actually using the cache, you operate on an instance of
Cache. EachCacheinstance has a unique identifier, which can be used as a namespace to distinguish different modules.
Class Methods
constructor()
Signature
constructor(options?: CacheManagerOptions)
Types
Details
CacheManagerOptions
StoreOptions
Default options
The options parameter will be merged with the default options. Properties already present in the default options can be omitted. For example:
registerStore()
Registers a new caching method. For example:
Signature
registerStore(options: { name: string } & StoreOptions)
createCache()
Creates a cache. For example:
Signature
createCache(options: { name: string; prefix?: string; store?: string; [key: string]: any }): Promise<Cache>
Details
options
If store is omitted, defaultStore will be used. In this case, the caching method will change according to the system's default caching method.
When there are no custom configurations, it returns the default cache space created by the global configuration and shared by the current caching method. It is recommended to add a prefix to avoid key conflicts.
Cache
See Cache
getCache()
Gets the corresponding cache.
Signature
getCache(name: string): Cache
flushAll()
Resets all caches.
close()
Closes all cache middleware connections.

