Skip to main content

Caching

Prism provides two caching layers: PrismCache for general-purpose key-value caching, and PrismResponseCacheMiddleware for automatic HTTP response caching with ETag and Cache-Control support.

PrismCache

An actor-based LRU cache with time-to-live eviction:
Basic Cache Usage

Cache Operations

Cache API

LRU Eviction

When the cache exceeds maxEntries, the least recently used entries are evicted first. Accessing an entry via get promotes it to most-recently-used.
LRU Behavior

Response Cache Middleware

Automatically cache HTTP responses with proper ETag and Cache-Control headers:
Enable Response Caching

How It Works

  1. Cache MISS — request passes through, response is cached and returned with X-Cache: MISS, ETag, and Cache-Control headers
  2. Cache HIT — cached response returned immediately with X-Cache: HIT
  3. Conditional request — if client sends If-None-Match matching the ETag, returns 304 Not Modified (no body)

Cache Headers

Custom Cache Predicate

By default, only GET requests are cached. Customize this:
Custom Predicate

Practical Example: Expensive Query Cache

Cache Database Results
Put PrismResponseCacheMiddleware after authentication middleware but before route handlers. This way, only authenticated requests are cached, and the cache key includes the full URI (path + query string).
Don’t cache responses that contain user-specific data unless the cache key includes the user identifier. The default key is METHOD:URI — two different users requesting /profile would get the same cached response.