Caching and streaming¶
Conditional GET¶
fresh-when sets validators on the response and renders a 304 Not Modified
when the request's If-None-Match/If-Modified-Since show the client already
has the current version. is-stale is its inverse: it returns true when you
should render.
1 2 3 | |
etag-for computes an ETag from a value, using its cache-key or to-hash when
available. ETags are weak by default (W/"..."); pass weak => False for a
strong ETag. Matching uses a weak comparison, so W/ prefixes are ignored, and
* matches anything.
Cache headers¶
expires-in sets Cache-Control with a max-age, private by default or
public, plus any extra directives. expires-now marks the response
uncacheable:
1 2 3 4 5 | |
Cache store¶
MVC::Keayl::Cache provides a Store role with a low-level key-value API shared
by every backend:
1 2 3 4 5 6 | |
fetch returns a cached value or computes, stores, and returns it. force
recomputes, skip-nil declines to store an undefined result, and
race-condition-ttl serves the stale value to other readers while one recomputes
an expired entry.
increment and decrement maintain counters, preserving an entry's expiry
window across updates. read-multi and write-multi work on several keys at
once, and delete-matched removes every key matching a string glob or a regex:
1 2 3 4 | |
Entries carry an optional version; a read with a mismatched version misses.
A store can take a namespace that prefixes its keys and a default-expires-in
applied when a write gives no expiry.
Backends¶
MemoryStorekeeps entries in process, with an optionalmax-entriesbound that evicts the least recently used entry.FileStorepersists each entry under arootdirectory, surviving across instances.NullStorestores nothing, sofetchalways recomputes. It is the no-op backend for test and development.ExternalStoredelegates to an injectedclientshaped like a Redis or Memcache driver (get,setwith attl,del,keys), serializing each entry through it.
Fragment caching¶
A store's fetch backs view fragment caching. cache-key derives a key from
parts (an object contributes its own cache-key), under a views/ namespace,
with an optional template digest:
1 | |
A view caches a fragment with cache-fragment, which computes the content once
per key through its configured store:
1 | |
Rate limiting¶
rate-limit is a controller class method that caps how often a client may reach
an action, backed by a cache store. It registers a before-action that counts
requests per discriminator within a window and blocks once the count passes the
limit:
1 | |
It also has an is rate-limit trait form for the class header:
1 | |
By default the discriminator is the request's remote IP and an over-limit request
gets 429 Too Many Requests with a Retry-After header set to the window. by
supplies a custom discriminator and with a custom over-limit handler; store
and name choose the backing store and the counter name:
1 2 3 4 5 6 | |
Streaming¶
A response body can be streamed instead of buffered. stream takes an iterable
of chunks (strings or blobs); stream-chunks yields them encoded, and
is-streaming reports whether a stream is set, for an adapter to write chunked:
1 | |
This is the pull-based primitive: the whole sequence is known up front. For a push-based response, where an action writes chunks over time, see live streaming.
Range requests¶
send-file honours a Range request header, responding 206 Partial Content
with a Content-Range header and the requested byte slice, and advertises
Accept-Ranges: bytes.