CacheHTTP Response Caching per RFC 9111
This module provides an in-memory cache for HTTP responses following RFC 9111 (HTTP Caching). It handles cache storage, validation, and freshness calculations.
Cache entries are keyed by method and effective request URI per RFC 9111. Only safe methods (GET, HEAD) are cached by default.
(* Create an in-memory cache *)
let cache = Cache.Memory.create ~max_entries:1000 () in
(* Store a response *)
Cache.Memory.store cache ~key ~response ~request_time ~response_time;
(* Lookup a cached response *)
match Cache.Memory.lookup cache ~key ~now with
| Some (entry, status) ->
Printf.printf "Cache %s: %s\n"
(match status with `Fresh -> "hit" | `Stale -> "stale")
entry.url
| None -> Printf.printf "Cache miss\n"val src : Logs.Src.tLog source for cache operations
type entry = {url : string;The effective request URI
*)method_ : Method.t;HTTP method used for the request
*)status : int;Response status code
*)headers : Headers.t;Response headers
*)body : string;Response body
*)request_time : Ptime.t;When the request was initiated
*)response_time : Ptime.t;When the response was received
*)date_value : Ptime.t option;Parsed Date header value
*)age_value : int;Age header value (0 if not present)
*)cache_control : Cache_control.response;Parsed Cache-Control header
*)etag : string option;ETag header for validation
*)last_modified : string option;Last-Modified header for validation
*)vary_headers : (string * string) list;Request header values for Vary matching
*)freshness_lifetime : int option;Calculated freshness lifetime in seconds
*)}A cached response entry
type key = {method_key : Method.t;uri : string;vary_values : (string * string) list;Values of Vary headers from request
*)}A cache key for lookups
val make_key :
method_:Method.t ->
uri:string ->
?request_headers:Headers.t ->
?vary:string list ->
unit ->
keyCreate a cache key.
module Memory : sig ... endIn-memory HTTP response cache using a Hashtbl. Thread-safe using Eio.Mutex.
val needs_validation : entry -> boolCheck if a cached entry needs revalidation with the origin server. True if must-revalidate or no-cache is set.
Get headers to send for a conditional request. Includes If-None-Match (from ETag) and/or If-Modified-Since (from Last-Modified).
Check if a response indicates the cached version is still valid (304).
val vary_matches :
cached_vary:(string * string) list ->
request_headers:Headers.t ->
boolCheck if request headers match the cached Vary values.