Skip to content

Commit

Permalink
Cache ResourceFactory to make use of strong eTags when available
Browse files Browse the repository at this point in the history
  • Loading branch information
ok2c committed Jan 26, 2024
1 parent 5ecd396 commit d787637
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
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;
Expand Down Expand Up @@ -399,7 +400,11 @@ public Cancellable store(
}
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
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;
Expand Down Expand Up @@ -232,7 +233,11 @@ public CacheHit store(
}
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);
Expand Down

0 comments on commit d787637

Please sign in to comment.