Overview
TheCacheStore interface defines the contract for cache storage implementations. It provides the methods needed to store, retrieve, and manage cached data. You can implement this interface to create custom cache backends.
Interface: CacheStore
Required Methods
get()
Retrieves a value from the cache store.The normalized cache key to retrieve
The cached value as a string, or
undefined if not foundValues are stored as JSON strings. The
Cacher class handles serialization and deserialization.set()
Stores a value in the cache store.The normalized cache key to store the value under
The value to cache as a JSON string
Time-to-live in seconds. The implementation should expire the entry after this duration.
Returns
true if the operation was successful, false otherwisedel()
Deletes a value from the cache store.The normalized cache key to delete
Returns
true if the operation was successful, false otherwiseclear()
Clears all values from the cache store.Returns
true if the operation was successful, false otherwiseOptional Methods
These methods are optional but can improve performance when implemented natively by the store.getMultiple()
Retrieves multiple values from the cache store at once.Array of normalized cache keys to retrieve
Array of values in the same order as the keys
If not implemented, the
Cacher class will fall back to calling get() multiple times.setMultiple()
Stores multiple values in the cache store at once.Object mapping normalized cache keys to JSON string values
Time-to-live in seconds for all values
Returns
true if all operations were successful, false otherwisedelMultiple()
Deletes multiple values from the cache store at once.Array of normalized cache keys to delete
Returns
true if all operations were successful, false otherwisehas()
Checks if a key exists in the cache store.The normalized cache key to check
Returns
true if the key exists, false otherwisedispose()
Cleans up resources and closes connections.This method is called when the
Cacher instance is disposed. Use it to clean up connections, timers, or other resources.Built-in Implementations
The framework provides several built-in cache store implementations:- NullCache: A no-op cache that doesn’t store anything (default)
- MemoryCache: An in-memory cache using a Map
- FileCache: A file-system based cache (available in
@resolid/cache-file)
Custom Implementation Example
Source Code
Location:packages/cache/src/stores/types.ts