You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by mi...@apache.org on 2019/12/15 11:45:35 UTC

[httpcomponents-client] 01/01: HTTPCLIENT-2038: CacheConfig should use TimeValue

This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch HTTPCLIENT-2038
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git

commit a5dac4c6f2e4c6801f363f90eb27fea82ad6c61f
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Sun Dec 15 12:44:04 2019 +0100

    HTTPCLIENT-2038: CacheConfig should use TimeValue
---
 .../hc/client5/http/impl/cache/CacheConfig.java    | 32 +++++++++++-----------
 .../cache/CachedResponseSuitabilityChecker.java    |  5 ++--
 .../TestCachedResponseSuitabilityChecker.java      |  3 +-
 3 files changed, 21 insertions(+), 19 deletions(-)

diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheConfig.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheConfig.java
index 21c92fd..16c67a9 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheConfig.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheConfig.java
@@ -27,6 +27,7 @@
 package org.apache.hc.client5.http.impl.cache;
 
 import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.TimeValue;
 
 /**
  * <p>Java Beans-style configuration for caching {@link org.apache.hc.client5.http.classic.HttpClient}.
@@ -129,10 +130,10 @@ public class CacheConfig implements Cloneable {
      */
     public final static float DEFAULT_HEURISTIC_COEFFICIENT = 0.1f;
 
-    /** Default lifetime in seconds to be assumed when we cannot calculate
+    /** Default lifetime to be assumed when we cannot calculate
      * freshness heuristically.
      */
-    public final static long DEFAULT_HEURISTIC_LIFETIME = 0;
+    public final static TimeValue DEFAULT_HEURISTIC_LIFETIME = TimeValue.ZERO_MILLISECONDS;
 
     /** Default number of worker threads to allow for background revalidations
      * resulting from the stale-while-revalidate directive.
@@ -148,7 +149,7 @@ public class CacheConfig implements Cloneable {
     private final boolean weakETagOnPutDeleteAllowed;
     private final boolean heuristicCachingEnabled;
     private final float heuristicCoefficient;
-    private final long heuristicDefaultLifetime;
+    private final TimeValue heuristicDefaultLifetime;
     private final boolean sharedCache;
     private final boolean freshnessCheckEnabled;
     private final int asynchronousWorkers;
@@ -162,7 +163,7 @@ public class CacheConfig implements Cloneable {
             final boolean weakETagOnPutDeleteAllowed,
             final boolean heuristicCachingEnabled,
             final float heuristicCoefficient,
-            final long heuristicDefaultLifetime,
+            final TimeValue heuristicDefaultLifetime,
             final boolean sharedCache,
             final boolean freshnessCheckEnabled,
             final int asynchronousWorkers,
@@ -250,7 +251,7 @@ public class CacheConfig implements Cloneable {
      * Get the default lifetime to be used if heuristic freshness calculation is
      * not possible.
      */
-    public long getHeuristicDefaultLifetime() {
+    public TimeValue getHeuristicDefaultLifetime() {
         return heuristicDefaultLifetime;
     }
 
@@ -315,7 +316,7 @@ public class CacheConfig implements Cloneable {
         private boolean weakETagOnPutDeleteAllowed;
         private boolean heuristicCachingEnabled;
         private float heuristicCoefficient;
-        private long heuristicDefaultLifetime;
+        private TimeValue heuristicDefaultLifetime;
         private boolean sharedCache;
         private boolean freshnessCheckEnabled;
         private int asynchronousWorkers;
@@ -404,17 +405,16 @@ public class CacheConfig implements Cloneable {
         }
 
         /**
-         * Sets default lifetime in seconds to be used if heuristic freshness
-         * calculation is not possible. Explicit cache control directives on
-         * either the request or origin response will override this, as will
-         * the heuristic {@code Last-Modified} freshness calculation if it is
-         * available.
-         * @param heuristicDefaultLifetime is the number of seconds to
-         *   consider a cache-eligible response fresh in the absence of other
-         *   information. Set this to {@code 0} to disable this style of
-         *   heuristic caching.
+         * Sets default lifetime to be used if heuristic freshness calculation
+         * is not possible. Explicit cache control directives on either the
+         * request or origin response will override this, as will the heuristic
+         * {@code Last-Modified} freshness calculation if it is available.
+         *
+         * @param heuristicDefaultLifetime is the number to consider a
+         *   cache-eligible response fresh in the absence of other information.
+         *   Set this to {@code 0} to disable this style of heuristic caching.
          */
-        public Builder setHeuristicDefaultLifetime(final long heuristicDefaultLifetime) {
+        public Builder setHeuristicDefaultLifetime(final TimeValue heuristicDefaultLifetime) {
             this.heuristicDefaultLifetime = heuristicDefaultLifetime;
             return this;
         }
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
index 3a5d1a9..eb5210e 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
@@ -38,6 +38,7 @@ import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.HttpStatus;
 import org.apache.hc.core5.http.message.MessageSupport;
+import org.apache.hc.core5.util.TimeValue;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -52,7 +53,7 @@ class CachedResponseSuitabilityChecker {
     private final boolean sharedCache;
     private final boolean useHeuristicCaching;
     private final float heuristicCoefficient;
-    private final long heuristicDefaultLifetime;
+    private final TimeValue heuristicDefaultLifetime;
     private final CacheValidityPolicy validityStrategy;
 
     CachedResponseSuitabilityChecker(final CacheValidityPolicy validityStrategy,
@@ -74,7 +75,7 @@ class CachedResponseSuitabilityChecker {
             return true;
         }
         if (useHeuristicCaching &&
-                validityStrategy.isResponseHeuristicallyFresh(entry, now, heuristicCoefficient, heuristicDefaultLifetime)) {
+                validityStrategy.isResponseHeuristicallyFresh(entry, now, heuristicCoefficient, heuristicDefaultLifetime.toSeconds())) {
             return true;
         }
         if (originInsistsOnFreshness(entry)) {
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedResponseSuitabilityChecker.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedResponseSuitabilityChecker.java
index 65b28b7..5640156 100644
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedResponseSuitabilityChecker.java
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedResponseSuitabilityChecker.java
@@ -35,6 +35,7 @@ import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.message.BasicHeader;
 import org.apache.hc.core5.http.message.BasicHttpRequest;
+import org.apache.hc.core5.util.TimeValue;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -243,7 +244,7 @@ public class TestCachedResponseSuitabilityChecker {
 
         final CacheConfig config = CacheConfig.custom()
             .setHeuristicCachingEnabled(true)
-            .setHeuristicDefaultLifetime(20)
+            .setHeuristicDefaultLifetime(TimeValue.ofSeconds(20L))
             .build();
         impl = new CachedResponseSuitabilityChecker(config);