Cache ResourceFactory to make use of strong eTags when available

This commit is contained in:
Oleg Kalnichevski 2024-01-11 15:21:47 +01:00
parent 5ecd396f60
commit d787637e6e
3 changed files with 29 additions and 2 deletions

View File

@ -60,6 +60,23 @@ public interface ResourceFactory {
*/ */
Resource generate(String requestId, byte[] content, int off, int len) throws ResourceIOException; 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. * @deprecated Do not use.
*/ */

View File

@ -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.cache.ResourceIOException;
import org.apache.hc.client5.http.impl.Operations; import org.apache.hc.client5.http.impl.Operations;
import org.apache.hc.client5.http.validator.ETag; 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.CallbackContribution;
import org.apache.hc.core5.concurrent.Cancellable; import org.apache.hc.core5.concurrent.Cancellable;
import org.apache.hc.core5.concurrent.ComplexCancellable; import org.apache.hc.core5.concurrent.ComplexCancellable;
@ -399,7 +400,11 @@ class BasicHttpAsyncCache implements HttpAsyncCache {
} }
final Resource resource; final Resource resource;
try { 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) { } catch (final ResourceIOException ex) {
if (LOG.isWarnEnabled()) { if (LOG.isWarnEnabled()) {
LOG.warn("I/O error creating cache entry with key {}", rootKey); LOG.warn("I/O error creating cache entry with key {}", rootKey);

View File

@ -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.ResourceFactory;
import org.apache.hc.client5.http.cache.ResourceIOException; import org.apache.hc.client5.http.cache.ResourceIOException;
import org.apache.hc.client5.http.validator.ETag; 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.HttpHeaders;
import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest; import org.apache.hc.core5.http.HttpRequest;
@ -232,7 +233,11 @@ class BasicHttpCache implements HttpCache {
} }
final Resource resource; final Resource resource;
try { 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) { } catch (final ResourceIOException ex) {
if (LOG.isWarnEnabled()) { if (LOG.isWarnEnabled()) {
LOG.warn("I/O error creating cache entry with key {}", rootKey); LOG.warn("I/O error creating cache entry with key {}", rootKey);