Skip to content

[ISSUE #6505][ISSUE #6506] Preserve HTTP response cache semantics - #7662

Merged
Aias00 merged 2 commits into
apache:masterfrom
yangjj-iso:feat/6506-cache-bodyless-get-requests
Oct 16, 2026
Merged

Aias00 merged 2 commits into
apache:masterfrom
yangjj-iso:feat/6506-cache-bodyless-get-requests

Conversation

@yangjj-iso

@yangjj-iso yangjj-iso commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #6505.
Fixes #6506.

Cache hits previously replayed only the body and Content-Type, so a cached redirect lost Location/status and an error body could become a default 200 response. The path/query-only key also reused responses across HTTP methods, request bodies, authorities, encoded query strings, and matched rules.

Restrict cache use to bodyless GET requests, forwarding other requests normally. Requests carrying credentials/cookies, range or conditional headers, or client cache directives bypass both reads and writes. Inspect the actual GET body stream, replay its first buffer downstream when data is present, and release peeked pooled buffers on completion/error/cancellation. Keys include the method, complete raw URI, selector ID, and rule ID.

Store one complete response value under a separate versioned key: a length-prefixed metadata document followed by raw body bytes. Capture final application headers/status at commit after preceding callbacks, and publish the entry only after the response write succeeds. Hits restore status and end-to-end headers, preserve multi-value headers and missing Content-Type, recalculate Content-Length, and support empty/204 responses. Connection headers and Connection-nominated headers are excluded. Repeated completion is idempotent, and cache-write failures do not replace the upstream response.

Responses with cookies, Vary, private/no-store/no-cache directives, or informational/partial/not-modified statuses are not stored. Streaming writeAndFlushWith responses pass through. Invalid entries are treated as misses; old body-only/body-plus-type entries are not reused and expire under their existing TTL. The ICache backend SPI is unchanged. docs/cache-request-eligibility.md documents these behavior changes and the cold-cache transition; POST and other non-GET routes now call upstream on every request.

Validation:

  • Regression tests reproduce different POST bodies sharing a response, GET bodies reading old entries, lost status/headers, and same-URL cross-rule reuse before the relevant fixes.
  • All 90 selected tests pass: 89 cache-handler tests and 1 memory-cache test, with zero failures/errors/skips.
  • Coverage includes request isolation, status/redirect/header replay, cookies and commit-time restrictions, conditions/ranges, empty responses and repeated completion, failed response writes, malformed entries, binary-body encoding, and pooled-buffer ownership.
  • Checkstyle and Apache RAT pass across the affected reactor; git diff --check passes.
  • Full-repository install and a deployed gateway/Redis cluster were not run locally.
./mvnw -B -ntp -pl shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler -am test \
  '-Dtest=CachedResponseTest,CacheResponseReplayTest,CacheRequestEligibilityTest,CacheUtilsTest,CachePlugin*Test,ApplicationConfigCacheTest,MemoryCacheTest' \
  -Dsurefire.failIfNoSpecifiedTests=false -DskipRemoteResources=true \
  -Djacoco.skip=true -Dmaven.javadoc.skip=true
./mvnw -B -ntp -pl shenyu-plugin/shenyu-plugin-cache/shenyu-plugin-cache-handler -am apache-rat:check -DskipRemoteResources=true
  • Read the contribution guidelines.
  • Submit regression tests.
  • Full local ./mvnw clean install -Dmaven.javadoc.skip=true (affected-reactor validation above).

@yangjj-iso yangjj-iso changed the title [ISSUE #6506] Restrict response caching to bodyless GET requests [ISSUE #6505][ISSUE #6506] Preserve HTTP response cache semantics Oct 15, 2026

@Aias00 Aias00 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.

Review: Preserve HTTP response cache semantics (#6505 / #6506)

This is a thorough, well-engineered rewrite of the response-cache plugin. Approach and quality are strong.

What's good

  • Correct cache-eligibility gate. doExecute now short-circuits to the chain for non-GET methods, requests with Content-Length > 0 / Transfer-Encoding, and any of Authorization/Cookie/Range/If-*/Cache-Control/Pragma. This fixes the previous unsafe behavior where POST/PUT/etc. could be cached and served cross-request.
  • switchOnFirst body peeking. A GET that actually carries a body is forwarded (re-wrapped via ServerHttpRequestDecorator) without ever aggregating or caching it, and a request-body error is propagated instead of being masked by a stale hit. Nice correctness touch.
  • Atomic complete-response envelope. New CachedResponse stores status + end-to-end headers + raw body as one versioned value (length-prefixed JSON metadata + bytes). This preserves redirects, error statuses, multi-value headers, absent Content-Type, and 204 — none of which the old split dataKey/contentTypeKey scheme handled reliably.
  • Safe capture timing. Snapshot is taken in beforeCommit and persisted only after the write succeeds (doOnSuccess), with hop-by-hop headers, Set-Cookie/Vary/restricted Cache-Control, 206/304, and streaming (writeAndFlushWith) correctly excluded. Content-Length is recalculated.
  • Key isolation. responseKey folds method + raw URI + matched selector + rule id, and decode rejects legacy body-only / malformed envelopes as misses — no Redis migration needed, no cross-rule entry sharing.
  • Test coverage is excellent: CacheRequestEligibilityTest, CacheResponseReplayTest, CachedResponseTest, CacheUtilsTest plus updated CachePluginTest cover eligibility, status/header/body replay, cookie/vary/private exclusion, empty responses, malformed-envelope fallback, and key dimensionality.

Minor / non-blocking

  • CacheUtils.contentTypeKey(...) is now unused by production code (only referenced by one test assertion testNonGetMissDoesNotWriteCache). Safe to leave, but could be removed to avoid dead surface area.
  • dataKey still exists for backwards compatibility; worth a short comment clarifying it is only a building block of responseKey now, not a standalone cache entry.

Conclusion

Solid, safe, and well-tested. Approving. The only follow-up is to re-run the infra-level e2e / registry_dependencies jobs (which are currently red but unrelated to this change — they fail on the compose/registry setup).

Great work.

@Aias00
Aias00 merged commit 58b7d79 into apache:master Oct 16, 2026
28 checks passed
@Aias00 Aias00 added this to the 2.7.2 milestone Nov 13, 2026
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.

[BUG] Cache plugin key ignores HTTP method and request body [BUG] Cache plugin loses response status and headers on cache hit

2 participants