Caching Decorators
Define TypeScript caching strategies, eviction policies, cache updates, invalidation, and stampede protection with AxilJS semantic decorators.
Caching Decorators
Caching decorators define how method results are cached, updated, evicted, and protected from concurrent cache misses. They keep cache strategy separate from business logic while exposing the intended semantics to independent consumers.
@tCache
@tCache caches a method's return value under a specified key with a time-to-live (TTL).
Options
| Option | Type | Description |
|---|---|---|
key | string | Function | Cache key |
ttl | number | Time to live in milliseconds |
tags | string[] | Tags used for group invalidation |
condition | Function | Predicate that determines whether the result should be cached |
These options allow cache identity, expiration, grouping, and conditional caching to be declared independently of the method implementation.
@tCacheEvict
@tCacheEvict removes cached entries when the decorated method executes.
Use a specific key when a single cache entry must be removed:
You can also evict entries associated with a tag:
@tCachePut
@tCachePut updates the cache using the decorated method's return value.
This expresses an explicit cache-refresh operation rather than relying on a cache read to populate the entry.
@tCacheInvalidate
@tCacheInvalidate invalidates all cache entries associated with the specified tags.
This is useful when one operation affects multiple cached resources and invalidating individual keys would be impractical.
@tCacheStampede
A cache stampede can occur when an expired or missing cache entry causes many concurrent requests to recompute the same expensive result.
@tCacheStampede declares how those concurrent misses should be handled:
AxilJS defines three stampede-protection strategies:
| Strategy | Description |
|---|---|
'single-flight' | Coalesce concurrent cache misses into one call |
'lock' | Acquire a lock before recomputing the value |
'stale-while-revalidate' | Serve stale data while refreshing it in the background |
Combining Cache Semantics
Caching decorators can be composed with HTTP and database semantics to describe the complete behavior of an operation.
The first method declares a cached database read with single-flight stampede protection. The second declares a write that evicts the affected cache entry and associated product entries.
Choosing the Right Decorator
Use @tCache when the result should be cached with a key and TTL.
Use @tCacheEvict when an operation should remove existing cache entries.
Use @tCachePut when an operation should update the cache with its returned value.
Use @tCacheInvalidate when multiple entries should be invalidated through tags.
Use @tCacheStampede when concurrent cache misses could trigger expensive duplicate work.
These decorators describe cache intent. The actual cache implementation is handled by the consumer that interprets the metadata, keeping caching semantics separate from application business logic.
Reference
| Decorator | Purpose |
|---|---|
tCache | Cache method results with a key and TTL |
tCacheEvict | Remove cached entries |
tCachePut | Update the cache with a method result |
tCacheInvalidate | Invalidate entries by tags |
tCacheStampede | Protect against concurrent cache misses |
Caching is one part of AxilJS's broader semantic decorator model. The decorator writes metadata; independent consumers interpret that metadata and implement the corresponding runtime behavior.