Skip to content

Added support for request body to the cache key generator#850

Open
ok2c wants to merge 1 commit into
apache:masterfrom
ok2c:feature/cache-key-generator-body-support
Open

Added support for request body to the cache key generator#850
ok2c wants to merge 1 commit into
apache:masterfrom
ok2c:feature/cache-key-generator-body-support

Conversation

@ok2c

@ok2c ok2c commented Jul 15, 2026

Copy link
Copy Markdown
Member

This change-set extends the cache key generator with support for requests with an enclosed content body such as QUERY.

@desiderantes Please review and let me know if this approach works for you.

@arturobernalg Please double-check.

@ok2c ok2c force-pushed the feature/cache-key-generator-body-support branch from e6eb8a8 to 9be506c Compare July 15, 2026 16:17
final Function<T, String> bodyExtractor) {
final String s = CacheSupport.requestUriRaw(host, request);
final String body = bodyExtractor != null ? bodyExtractor.apply(request) : null;
if (body != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it supposed to fail now if the body extractor succeeds?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good, just wanted to confirm

@desiderantes desiderantes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's solid and simple, thank you! I'll rebase my PR and simplify it after this lands.

if (hit == null) {
if (requestCollapsingEnabled && root == null && !requestCacheControl.isOnlyIfCached()) {
final String cacheKey = CacheKeyGenerator.INSTANCE.generateKey(target, cacheRequest);
final String cacheKey = CacheKeyGenerator.INSTANCE.generateKey(target, cacheRequest, SimpleHttpRequest::getBodyText);

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... it is also possible that subclasses of Request might have a certain representation for certain content types (like a sorted object/array for json, or ordered form fields, etc), which might affect the caching as different bodies should be cached because they converge to the same canonical representation

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That still does not require String. A request implementation could canonicalize the content and return the resulting bytes. That preserves the option for content-type-specific normalization without forcing arbitrary content through text decoding.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, not all content is text-like, sorry, this is a good suggestion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants