TagCache class
Tagged cache over redis: values are stored under their own keys, and each key is additionally added to a redis set per tag. Invalidating a tag then drops every value that was stored with it, which is what plain key-based caching cannot express — one write can be invalidated by any of several unrelated events.
The typical use is caching a computed result that depends on several entities and dropping it when any one of them changes:
import { RedisCache } from '@imqueue/rpc';
import { TagCache } from '@imqueue/tag-cache';
const cache = new TagCache(await new RedisCache().init({ prefix: 'app' }));
await cache.set('user:1:invoices', invoices, ['user:1', 'invoices'], 60000);
// later, when user 1 changes — drops the entry above and anything else
// tagged 'user:1', whatever key it was stored under
await cache.invalidate('user:1');
Two things to know before relying on it. Read and write operations do NOT throw on a redis failure: they log a warning and report the failure in their return value, so a cache outage degrades to cache misses instead of taking the caller down. And the underlying redis connection is shared and owned by RedisCache, so TagCache.destroy() tears it down for every instance — see that method.
Signature:
export declare class TagCache
Constructors
|
Constructor |
Modifiers |
Description |
|---|---|---|
|
Constructs a new instance of the |
Properties
|
Property |
Modifiers |
Type |
Description |
|---|---|---|---|
|
RedisCache | undefined |
(Optional) The | ||
|
|
(key: string) => string |
Maps a caller-supplied key onto the fully-qualified redis key, applying the prefix the underlying | |
|
ILogger |
Logger inherited from the underlying | ||
|
Redis |
(Optional) Shared |
Methods
|
Method |
Modifiers |
Description |
|---|---|---|
|
Destroys this cache instance Note the connection is owned by Afterwards this instance keeps no redis reference, so every operation on it throws a | ||
|
Returns data stored under given keys. If a single key provided returns a single result, otherwise it will return an array of results associated with the keys Values are JSON-decoded on the way out, so what comes back is what was passed to TagCache.set(), not a string. A redis failure is not thrown: it is logged as a warning and reported as | ||
|
Invalidates data under given tags Collects every key held by the given tags, deletes those keys, and then removes them from all other tag sets so no tag is left pointing at a key that no longer exists. Two properties worth knowing, because neither is obvious from the signature:
| ||
|
Stores given value under a given key, tagging it with the given tags The value is JSON-encoded, and the key is added to one redis set per tag so TagCache.invalidate() can find it later. Everything happens in a single When |
Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.