public TimeValue getFreshnessLifetime(final ResponseCacheControl responseCacheControl, final HttpCacheEntry entry) {
        // If the cache is shared and the s-maxage response directive is present, use its value
        if (shared) {
            final long sharedMaxAge = responseCacheControl.getSharedMaxAge();
            if (sharedMaxAge > -1) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Using s-maxage directive for freshness lifetime calculation: {} seconds", sharedMaxAge);
                }
                return TimeValue.ofSeconds(sharedMaxAge);
            }
        }

        // If the max-age response directive is present, use its value
        final long maxAge = responseCacheControl.getMaxAge();
        if (maxAge > -1) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Using max-age directive for freshness lifetime calculation: {} seconds", maxAge);
            }
            return TimeValue.ofSeconds(maxAge);
        }

        // If the Expires response header field is present, use its value minus the value of the Date response header field
        final Instant dateValue = entry.getInstant();
        if (dateValue != null) {
            final Instant expiry = entry.getExpires();
            if (expiry != null) {
                final Duration diff = Duration.between(dateValue, expiry);
                if (diff.isNegative()) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Negative freshness lifetime detected. Content is already expired. Returning zero freshness lifetime.");
                    }
                    return TimeValue.ZERO_MILLISECONDS;
                }
                return TimeValue.ofSeconds(diff.getSeconds());
            }
        }

        if (useHeuristicCaching) {
            // No explicit expiration time is present in the response. A heuristic freshness lifetime might be applicable
            if (LOG.isDebugEnabled()) {
                LOG.debug("No explicit expiration time present in the response. Using heuristic freshness lifetime calculation.");
            }
            return getHeuristicFreshnessLifetime(entry);
        }
        return TimeValue.ZERO_MILLISECONDS;
    }
Calculate the freshness lifetime of a response based on the provided cache control and cache entry.
<ul>
<li>If the cache is shared and the s-maxage response directive is present, use its value.</li>
<li>If the max-age response directive is present, use its value.</li>
<li>If the Expires response header field is present, use its value minus the value of the Date response header field.</li>
<li>Otherwise, a heuristic freshness lifetime might be applicable.</li>
</ul>

@param responseCacheControl the cache control directives associated with the response.
@param entry                the cache entry associated with the response.
@return the calculated freshness lifetime as a {@link TimeValue}.
httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java