Class ResponseCachingPolicy

java.lang.Object
org.apache.hc.client5.http.impl.cache.ResponseCachingPolicy

class ResponseCachingPolicy extends Object
  • Field Details

    • DEFAULT_FRESHNESS_DURATION

      private static final Duration DEFAULT_FRESHNESS_DURATION
      The default freshness duration for a cached object, in seconds.

      This constant is used to set the default value for the freshness lifetime of a cached object. When a new object is added to the cache, it will be assigned this duration if no other duration is specified.

      By default, this value is set to 300 seconds (5 minutes). Applications can customize this value as needed.

    • LOG

      private static final org.slf4j.Logger LOG
    • sharedCache

      private final boolean sharedCache
    • neverCache1_0ResponsesWithQueryString

      private final boolean neverCache1_0ResponsesWithQueryString
    • neverCache1_1ResponsesWithQueryString

      private final boolean neverCache1_1ResponsesWithQueryString
  • Constructor Details

    • ResponseCachingPolicy

      public ResponseCachingPolicy(boolean sharedCache, boolean neverCache1_0ResponsesWithQueryString, boolean neverCache1_1ResponsesWithQueryString)
      Constructs a new ResponseCachingPolicy with the specified cache policy settings and stale-if-error support.
      Parameters:
      sharedCache - whether to behave as a shared cache (true) or a non-shared/private cache (false)
      neverCache1_0ResponsesWithQueryString - true to never cache HTTP 1.0 responses with a query string, false to cache if explicit cache headers are found.
      neverCache1_1ResponsesWithQueryString - true to never cache HTTP 1.1 responses with a query string, false to cache if explicit cache headers are found.
      Since:
      5.4
  • Method Details

    • isResponseCacheable

      public boolean isResponseCacheable(ResponseCacheControl cacheControl, org.apache.hc.core5.http.HttpRequest request, org.apache.hc.core5.http.HttpResponse response)
      Determine if the HttpResponse gotten from the origin is a cacheable response.
      Returns:
      true if response is cacheable
    • isKnownCacheableStatusCode

      private static boolean isKnownCacheableStatusCode(int status)
    • isKnownNonCacheableStatusCode

      private static boolean isKnownNonCacheableStatusCode(int status)
    • isUnknownStatusCode

      private static boolean isUnknownStatusCode(int status)
    • isExplicitlyNonCacheable

      protected boolean isExplicitlyNonCacheable(ResponseCacheControl cacheControl)
      Determines whether the given CacheControl object indicates that the response is explicitly non-cacheable.
      Parameters:
      cacheControl - the CacheControl object representing the cache-control directive(s) from the HTTP response.
      Returns:
      true if the response is explicitly non-cacheable according to the cache-control directive(s), false otherwise.

      When cacheControl is non-null: - Returns true if the response contains "no-store" or (if sharedCache is true) "private" cache-control directives. - If the response contains the "no-cache" directive, it is considered cacheable, but requires validation against the origin server before use. In this case, the method returns false. - Returns false for other cache-control directives, implying the response is cacheable.

      When cacheControl is null, returns false, implying the response is cacheable.

    • isExplicitlyCacheable

      protected boolean isExplicitlyCacheable(ResponseCacheControl cacheControl, org.apache.hc.core5.http.HttpResponse response)
    • isHeuristicallyCacheable

      protected boolean isHeuristicallyCacheable(ResponseCacheControl cacheControl, int status, Instant responseDate, Instant responseExpires)
    • expiresHeaderLessOrEqualToDateHeaderAndNoCacheControl

      private boolean expiresHeaderLessOrEqualToDateHeaderAndNoCacheControl(ResponseCacheControl cacheControl, Instant responseDate, Instant expires)
    • from1_0Origin

      private boolean from1_0Origin(org.apache.hc.core5.http.HttpResponse response)
    • calculateFreshnessLifetime

      private Duration calculateFreshnessLifetime(ResponseCacheControl cacheControl, Instant responseDate, Instant responseExpires)
      Calculates the freshness lifetime of a response, based on the headers in the response.

      This method follows the algorithm for calculating the freshness lifetime. The freshness lifetime represents the time interval in seconds during which the response can be served without being considered stale. The freshness lifetime calculation takes into account the s-maxage, max-age, Expires, and Date headers as follows:

      • If the s-maxage directive is present in the Cache-Control header of the response, its value is used as the freshness lifetime for shared caches, which typically serve multiple users or clients.
      • If the max-age directive is present in the Cache-Control header of the response, its value is used as the freshness lifetime for private caches, which serve a single user or client.
      • If the Expires header is present in the response, its value is used as the expiration time of the response. The freshness lifetime is calculated as the difference between the expiration time and the time specified in the Date header of the response.
      • If none of the above headers are present or if the calculated freshness lifetime is invalid, a default value of 5 minutes is returned.

      Note that caching is a complex topic and cache control directives may interact with each other in non-trivial ways. This method provides a basic implementation of the freshness lifetime calculation algorithm and may not be suitable for all use cases. Developers should consult the HTTP caching specifications for more information and consider implementing additional caching mechanisms as needed.

    • understoodStatusCode

      private boolean understoodStatusCode(int status)
      Understood status codes include: - All 2xx (Successful) status codes (200-299) - All 3xx (Redirection) status codes (300-399) - All 4xx (Client Error) status codes up to 417 and 421 - All 5xx (Server Error) status codes up to 505
      Parameters:
      status - The HTTP status code to be checked.
      Returns:
      true if the HTTP status code is understood, false otherwise.
    • responseIsStillFresh

      private boolean responseIsStillFresh(Instant responseDate, Duration freshnessLifetime)
      Determines if an HttpResponse is still fresh based on its Date header and calculated freshness lifetime.

      This method calculates the age of the response from its Date header and compares it with the provided freshness lifetime. If the age is less than the freshness lifetime, the response is considered fresh.

      Note: If the Date header is missing or invalid, this method assumes the response is not fresh.

      Parameters:
      responseDate - The response date.
      freshnessLifetime - The calculated freshness lifetime of the HttpResponse.
      Returns:
      true if the response age is less than its freshness lifetime, false otherwise.