@grafctory/http - v0.9.0
    Preparing search index...

    Function cache

    • Creates a caching middleware.

      The generated middleware replays the response it holds for a GET or HEAD exchange while that response is fresh. Freshness is the one the origin server states or, where it states none, the one its Last-Modified implies or the ttl option assumes. A response no longer fresh is revalidated with its stored validators, and its held content is replayed again when the origin server answers 304. Where nothing usable is held, the exchange is relayed to the wrapped Fetch implementation and the response is kept for later exchanges. A successful mutating request invalidates the entries for its target, so a client that reads and writes never serves what it overwrote.

      A response answered from the store restates the exchange as it was retrieved, its URL, status and reason phrase included, so that a client reading it draws the same conclusions it draws from a freshly retrieved one. The age is the one departure: an Age header field states how long the content has been held since the origin server delivered it, so that whoever receives it judges for itself how much of its freshness is left. A successful revalidation restarts the age from what the 304 states, as the delivery it confirms supersedes the one the entry was reporting.

      Invalidation gives up every variant held for a target and extends to the URIs named by the Location and Content-Location header fields of the response, when they share the origin of the target, so that a server reports what else a mutating request has changed. No other entry is given up: a target and its query string variants are distinct resources, and nothing states that one goes stale when another is written.

      Exchanges targeting a scheme other than http and https and exchanges stating a Range are relayed untouched and never stored, as are partial responses and, outside the targets the force option selects, responses setting a cookie, so that a complete response answering a complete request is the only thing ever replayed.

      A target the avoid option selects is relayed untouched and never stored, so that a resource the consumer holds back stays out of the cache whatever the origin server states about it. Invalidation is unaffected: a successful unsafe exchange for an avoided target still gives up the entries the response reports as changed.

      A target the force option selects is cached as though the origin server stated no caching directives, so that a source forbidding caching on every response is reused under the freshness the consumer assumes. A forced response is stored even where it sets a cookie or states no-store. Its Cache-Control directives and Expires field play no part in its freshness, which follows its Last-Modified or, failing that, ttl. The cookie it sets reaches the client on the original exchange alone and is never stored, so that no session outlives the exchange that opened it.

      Forcing settles whether and for how long a response is reused, not which requests it answers or what its status means. Unsuccessful, partial and unpredictably varying responses are still never stored, variants are still told apart by Vary, and a freshness is still assumed for the heuristically cacheable status codes alone. A target both options select is avoided, as keeping a resource out of the cache outweighs reusing it.

      With no positive ttl, a forced response reporting no usable Last-Modified is revalidated on every exchange. A forced source reporting neither a change time nor a validator is then downloaded again each time, so force pays off for it only alongside a positive ttl, which bounds how long its content is reused.

      Every numeric option is removed by a value less than or equal to 0, as the defaults do: entries are then retained for the life of the middleware and reuse rests on what the origin server reports alone.

      The ttl option states a single freshness budget, that is how long a response is worth reusing: the origin server overrides it by stating less, and it governs where the response reports nothing to derive a freshness from, so that a response carrying no expiration is reused rather than revalidated on every exchange. Freshness is assumed only for the status codes RFC 9110 § 15.1 defines as heuristically cacheable, and only where no Cache-Control no-cache, max-age, must-revalidate or proxy-revalidate directive and no Expires header field is stated: a stated expiration keeps winning, however short it is, except for the targets the force option selects.

      A response stating no expiration but reporting when its content last changed is reused for a tenth of the interval it had gone unchanged, so that a resource edited a minute ago is revalidated sooner than one untouched for a year; ttl caps that share as it caps a stated expiration, and supplies the freshness where no usable Last-Modified is reported.

      Parameters

      • options: {
            store?: number | Store | Bucket;
            ttl?: number;
            avoid?: Some<string>;
            force?: Some<string>;
        } = {}

        The caching options, all optional: with none given, exchanges are cached in memory under the freshness the origin server reports

        • Optional Readonlystore?: number | Store | Bucket

          The Store or Bucket to hold entries in, or the number of entries the default memory store is to retain, giving up the least recently used beyond that

        • Optional Readonlyttl?: number

          The freshness to assume where a response reports neither an expiration nor a change time and the cap on any freshness derived from what it does report, in milliseconds, measured from the instant the origin server generated the content rather than from the instant it was received

        • Optional Readonlyavoid?: Some<string>

          The glob patterns selecting the targets to be kept out of the cache, one or several, matched whole against the target URI: * and ? stand for a run of characters and for a single character within a path segment, ** for a run of characters across separators; with none given, every eligible target is cached

        • Optional Readonlyforce?: Some<string>

          The glob patterns selecting the targets to be cached whatever caching directives the origin server states, one or several, matched as avoid patterns are; with none given, every target is cached under the directives the origin server states; a target the avoid option also selects is kept out of the cache

      Returns Middleware

      A Middleware replaying eligible exchanges from the store