-
Notifications
You must be signed in to change notification settings - Fork 988
Added support for request body to the cache key generator #850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |
| import java.util.Spliterators; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.Function; | ||
| import java.util.stream.StreamSupport; | ||
|
|
||
| import org.apache.hc.client5.http.cache.HttpCacheEntry; | ||
|
|
@@ -72,6 +73,10 @@ public class CacheKeyGenerator implements Resolver<URI, String> { | |
| */ | ||
| public static final CacheKeyGenerator INSTANCE = new CacheKeyGenerator(); | ||
|
|
||
| /** | ||
| * @deprecated Do not use. | ||
| */ | ||
| @Deprecated | ||
| @Override | ||
| public String resolve(final URI uri) { | ||
| return generateKey(uri); | ||
|
|
@@ -135,9 +140,33 @@ public String generateKey(final URI requestUri) { | |
| * @param host The host for this request | ||
| * @param request the {@link HttpRequest} | ||
| * @return cache key | ||
| * | ||
| * @deprecated Use {@link #generateKey(HttpHost, HttpRequest, Function)} | ||
| */ | ||
| @Deprecated | ||
| public String generateKey(final HttpHost host, final HttpRequest request) { | ||
| return generateKey(host, request, r -> null); | ||
| } | ||
|
|
||
| /** | ||
| * Computes a root key for the given {@link HttpHost}, {@link HttpRequest} and | ||
| * request body that can be used as a unique identifier for cached resources. | ||
| * | ||
| * @param host The host for this request | ||
| * @param request the {@link HttpRequest} | ||
| * @param bodyExtractor function to extract request body. May be {@code null} | ||
| * @return cache key | ||
| * | ||
| * @since 5.7 | ||
| */ | ||
| public <T extends HttpRequest> String generateKey(final HttpHost host, | ||
| final T request, | ||
| final Function<T, String> bodyExtractor) { | ||
| final String s = CacheSupport.requestUriRaw(host, request); | ||
| final String body = bodyExtractor != null ? bodyExtractor.apply(request) : null; | ||
| if (body != null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it supposed to fail now if the body extractor succeeds?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @desiderantes This is a temporary sanity check as the caching layer simply does not work correctly with entity enclosing requests. This is something I presume you are going to fix in your pull request.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good, just wanted to confirm |
||
| throw new UnsupportedOperationException(); | ||
| } | ||
| try { | ||
| return generateKey(new URI(s)); | ||
| } catch (final URISyntaxException ex) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make more sense to use byte[] here and SimpleHttpRequest::getBodyBytes at the call sites? getBodyText() can collapse different binary or invalid byte sequences into the same String, which would make the eventual cache key ambiguous.