Overview
TheCacher class provides a unified interface for caching data with support for multiple storage backends through the CacheStore interface. It handles serialization, deserialization, TTL (time-to-live), and batch operations.
Class: Cacher
Constructor
Configuration options for the cacher instance
The cache store implementation to use. Defaults to
NullCache (no-op cache).Default time-to-live in seconds for cached items. If not specified, items may be cached indefinitely (depending on the store implementation).
Methods
get()
Retrieves a value from the cache.The cache key to retrieve
Optional default value to return if the key is not found
The cached value, default value, or undefined if not found
set()
Stores a value in the cache.The cache key to store the value under
The value to cache. Will be JSON-serialized before storage.
Time-to-live in seconds. Overrides the default TTL if provided.
Returns
true if the operation was successful, false otherwisedel()
Deletes a value from the cache.The cache key to delete
Returns
true if the operation was successful, false otherwiseclear()
Clears all values from the cache.Returns
true if the operation was successful, false otherwisegetMultiple()
Retrieves multiple values from the cache at once.Array of cache keys to retrieve
Optional default value for keys that are not found
Array of values in the same order as the keys. Missing keys return
undefined or the default value.setMultiple()
Stores multiple values in the cache at once.Object mapping cache keys to values
Time-to-live in seconds for all values
Returns
true if all operations were successful, false otherwisedelMultiple()
Deletes multiple values from the cache at once.Array of cache keys to delete
Returns
true if all operations were successful, false otherwisehas()
Checks if a key exists in the cache.The cache key to check
Returns
true if the key exists, false otherwisedispose()
Cleans up resources and closes connections to the cache store.Type Definitions
CacheOptions
Usage Example
Source Code
Location:packages/cache/src/index.ts