node-ts-cache
Simple and extensible caching module supporting decorators
Install
npm install --save node-ts-cache
Usage
With decorator
Caches function response using the given options. Works with different strategies and storages. Uses all arguments to build an unique key.
@Cache(strategy, options)
strategy
: A supported caching Strategyoptions
: Options passed to the strategy for this particular method
Note: @Cache always converts the method response to a promise because caching might be async.
; ;
Cache decorator generates cache key according to class name, class method and args (with JSON.stringify). If you want another key creation logic you can bypass key creation strategy to the Cache decorator.
; ;;
Directly
; ;
Strategies
ExpirationStrategy
Cached items expire after a given amount of time.
ttl
: (Default: 60) Number of seconds to expire the cachte itemisLazy
: (Default: true) If true, expired cache entries will be deleted on touch. If false, entries will be deleted after the given ttl.isCachedForver
: (Default: false) If true, cache entry has no expiration.
Storages
Note: For specific storages, client libraries must be installed:
Storage | Needed client library |
---|---|
RedisStorage | npm install redis |
MemoryStorage()
fileName: string
)
FsJsonStorage(clientOpts:
RedisClientOptions)
RedisStorage(Test
npm test