Comunica
    Preparing search index...

    Interface ICachePolicy<I>

    A cache policy represents the policy under which certain data can be cached. This is a generalization of the CachePolicy interface from the http-cache-semantics npm package.

    interface ICachePolicy<I> {
        responseHeaders: () => Headers;
        revalidatedPolicy: (
            revalidationInput: I,
            revalidationResponse: ICacheResponseHead,
        ) => Promise<IRevalidationPolicy<I>>;
        revalidationHeaders: (newInput: I) => Promise<Headers>;
        satisfiesWithoutRevalidation: (input: I) => Promise<boolean>;
        storable: () => boolean;
        timeToLive: () => number;
    }

    Type Parameters

    • I

      The type of input request this policy applies to.

    Implemented by

    Index

    Properties

    responseHeaders: () => Headers

    Returns updated, filtered set of response headers to return to clients receiving the cached response. This function is necessary, because proxies MUST always remove hop-by-hop headers (such as TE and Connection) and update response's Age to avoid doubling cache time. Example: cachedResponse.headers = cachePolicy.responseHeaders(cachedResponse);

    revalidatedPolicy: (
        revalidationInput: I,
        revalidationResponse: ICacheResponseHead,
    ) => Promise<IRevalidationPolicy<I>>

    Use this method to update the cache after receiving a new response from the origin server.

    revalidationHeaders: (newInput: I) => Promise<Headers>

    Returns updated, filtered set of request headers to send to the origin server to check if the cached response can be reused. These headers allow the origin server to return status 304 indicating the response is still fresh. All headers unrelated to caching are passed through as-is.

    Use this method when updating cache from the origin server.

    updateRequest.headers = cachePolicy.revalidationHeaders(updateRequest);
    
    satisfiesWithoutRevalidation: (input: I) => Promise<boolean>

    This is the most important method. Use this method to check whether a cached response is still fresh in the context of the new request.

    If it returns true, then the given request matches the original response this cache policy has been created with, and the response can be reused without contacting the server. Note that the old response can't be returned without being updated, see responseHeaders().

    If it returns false, then the response may not be matching at all (e.g. it's for a different URL or method), or may require to be refreshed first (see revalidationHeaders()).

    Type Declaration

      • (input: I): Promise<boolean>
      • Parameters

        • input: I

          The new input request.

        Returns Promise<boolean>

    storable: () => boolean

    Returns true if the response can be stored in a cache. If it's false then you MUST NOT store either the request or the response.

    timeToLive: () => number

    Returns approximate time in milliseconds until the response becomes stale (i.e. not fresh).

    After that time (when timeToLive() <= 0) the response might not be usable without revalidation. However, there are exceptions, e.g. a client can explicitly allow stale responses, so always check with satisfiesWithoutRevalidation().