You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2024/01/26 08:15:28 UTC

(httpcomponents-client) 02/03: Cache ResourceFactory to make use of strong eTags when available

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

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

commit d787637e6edb62f3745c44dcc8df25695a2fe255
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Thu Jan 11 15:21:47 2024 +0100

    Cache ResourceFactory to make use of strong eTags when available
---
 .../apache/hc/client5/http/cache/ResourceFactory.java   | 17 +++++++++++++++++
 .../hc/client5/http/impl/cache/BasicHttpAsyncCache.java |  7 ++++++-
 .../hc/client5/http/impl/cache/BasicHttpCache.java      |  7 ++++++-
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/ResourceFactory.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/ResourceFactory.java
index b5c6d4789..72340e3ba 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/ResourceFactory.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/cache/ResourceFactory.java
@@ -60,6 +60,23 @@ public interface ResourceFactory {
      */
     Resource generate(String requestId, byte[] content, int off, int len) throws ResourceIOException;
 
+    /**
+     * Creates a {@link Resource} from a given response body.
+     * @param requestId a unique identifier for this particular response body.
+     * @param eTag eTag Strong (unique) identifier for the resource entity
+     *             with the given requestId, or {@code null} when not given
+     *             or is weak (non-unique).
+     * @param content byte array that represents the origin HTTP response body.
+     * @param off   the start offset in the array.
+     * @param len   the number of bytes to read from the array.
+     * @return a {@code Resource} containing however much of
+     *   the response body was successfully read.
+     * @throws ResourceIOException
+     */
+    default Resource generate(String requestId, String eTag, byte[] content, int off, int len) throws ResourceIOException {
+        return generate(requestId, content, off, len);
+    }
+
     /**
      * @deprecated Do not use.
      */
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java
index 55af33e17..7a5157e9a 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpAsyncCache.java
@@ -46,6 +46,7 @@ import org.apache.hc.client5.http.cache.ResourceFactory;
 import org.apache.hc.client5.http.cache.ResourceIOException;
 import org.apache.hc.client5.http.impl.Operations;
 import org.apache.hc.client5.http.validator.ETag;
+import org.apache.hc.client5.http.validator.ValidatorType;
 import org.apache.hc.core5.concurrent.CallbackContribution;
 import org.apache.hc.core5.concurrent.Cancellable;
 import org.apache.hc.core5.concurrent.ComplexCancellable;
@@ -399,7 +400,11 @@ class BasicHttpAsyncCache implements HttpAsyncCache {
         }
         final Resource resource;
         try {
-            resource = content != null ? resourceFactory.generate(request.getRequestUri(), content.array(), 0, content.length()) : null;
+            final ETag eTag = ETag.get(originResponse);
+            resource = content != null ? resourceFactory.generate(
+                    rootKey,
+                    eTag != null && eTag.getType() == ValidatorType.STRONG ? eTag.getValue() : null,
+                    content.array(), 0, content.length()) : null;
         } catch (final ResourceIOException ex) {
             if (LOG.isWarnEnabled()) {
                 LOG.warn("I/O error creating cache entry with key {}", rootKey);
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java
index 274f099c1..0d808d278 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/BasicHttpCache.java
@@ -43,6 +43,7 @@ import org.apache.hc.client5.http.cache.Resource;
 import org.apache.hc.client5.http.cache.ResourceFactory;
 import org.apache.hc.client5.http.cache.ResourceIOException;
 import org.apache.hc.client5.http.validator.ETag;
+import org.apache.hc.client5.http.validator.ValidatorType;
 import org.apache.hc.core5.http.HttpHeaders;
 import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.HttpRequest;
@@ -232,7 +233,11 @@ class BasicHttpCache implements HttpCache {
         }
         final Resource resource;
         try {
-            resource = content != null ? resourceFactory.generate(request.getRequestUri(), content.array(), 0, content.length()) : null;
+            final ETag eTag = ETag.get(originResponse);
+            resource = content != null ? resourceFactory.generate(
+                    rootKey,
+                    eTag != null && eTag.getType() == ValidatorType.STRONG ? eTag.getValue() : null,
+                    content.array(), 0, content.length()) : null;
         } catch (final ResourceIOException ex) {
             if (LOG.isWarnEnabled()) {
                 LOG.warn("I/O error creating cache entry with key {}", rootKey);