Skip to content

feat: add Caching Filter pattern (#1266) - #3634

Open
tiennm99 wants to merge 1 commit into
iluwatar:masterfrom
tiennm99:feat/caching_filter_pattern
Open

tiennm99 wants to merge 1 commit into
iluwatar:masterfrom
tiennm99:feat/caching_filter_pattern

Conversation

@tiennm99

Copy link
Copy Markdown
Contributor

Closes #1266.

Adds the Caching Filter pattern: a filter in a request-processing chain that serves a cached response until it expires, and otherwise passes the request on and stores the response.

What is included

  • Chain: Filter, an immutable index-based FilterChain and a terminal Handler, in the Intercepting Filter style.
  • CachingFilter: on a fresh hit it answers without calling the rest of the chain; on a miss or an expired entry it proceeds and stores successful responses. Non-GET requests bypass the cache and evict the cached response for their path.
  • CacheStore: holds the entries and the TTL expiry policy, and drops expired entries on lookup.
  • Time comes from an injectable java.time.InstantSource, so expiry is tested without sleeping.
  • ProductCatalog counts its lookups, so App shows the cache saving work (5 requests, 3 back-end lookups).
  • README following the pattern template, class diagram (etc/caching-filter.urm.puml and .png), and 18 unit tests.

Scope choices

  • Only successful (200) GET responses are cached: other methods may change state, and caching a 404 could hide a resource that appears later.
  • The cache has no size limit, to keep the focus on expiry; the README lists this as a trade-off.

Verification

./mvnw -pl caching-filter clean verify license:check passes.

Add a caching-filter module: a filter in an intercepting-filter chain that serves a stored GET response until it expires, otherwise passes the request on and caches successful responses. Time comes from an injectable InstantSource so expiry is tested without sleeping. Includes README, PlantUML class diagram and unit tests.
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

PR Summary

Adds a new caching-filter module implementing the Intercepting Filter pattern. Introduces a CacheStore with TTL expiry, a CachingFilter that serves cached GET responses and stores fresh ones, and supporting components (Filter, FilterChain, Handler) plus a ProductCatalog backend. Time comes from an injectable InstantSource to enable expiry testing without sleeping. Includes README, UML diagrams, and unit tests; updates the root pom to include the module.

Changes

File Summary
caching-filter/README.md Documentation for the Caching Filter pattern, including intent, usage, TTL expiry, time source, and testing approach; includes diagrams.
caching-filter/etc/caching-filter.urm.png UML diagram image for the Caching Filter class relationships.
caching-filter/etc/caching-filter.urm.puml PlantUML diagram detailing Caching Filter components and relationships.
caching-filter/pom.xml Module POM for the caching-filter module, defining dependencies and build config.
caching-filter/src/main/java/com/iluwatar/cachingfilter/App.java Demo app wiring a FilterChain with a CachingFilter and ProductCatalog to demonstrate TTL expiry via manual time advancement.
caching-filter/src/main/java/com/iluwatar/cachingfilter/CacheStore.java In-memory cache storing responses with per-entry expiry; supports eviction by path and size().
caching-filter/src/main/java/com/iluwatar/cachingfilter/CachingFilter.java Implements GET caching, eviction on non-GET, and stores successful responses; uses an InstantSource for time.
caching-filter/src/main/java/com/iluwatar/cachingfilter/Filter.java Functional interface representing a request filter in the chain.
caching-filter/src/main/java/com/iluwatar/cachingfilter/FilterChain.java Manages an ordered list of filters and delegates to the next filter or the final handler.
caching-filter/src/main/java/com/iluwatar/cachingfilter/Handler.java Functional interface for the chain’s terminal handler.
caching-filter/src/main/java/com/iluwatar/cachingfilter/ProductCatalog.java Slow back-end catalog that looks up products and counts lookups to illustrate cache savings.
caching-filter/src/main/java/com/iluwatar/cachingfilter/Request.java Request data object with method and path; includes a GET factory for convenience.
caching-filter/src/main/java/com/iluwatar/cachingfilter/Response.java Response data object with status and body; defines OK and NOT_FOUND constants.
caching-filter/src/test/java/com/iluwatar/cachingfilter/AppTest.java JUnit test ensuring App.main runs without throwing exceptions.
caching-filter/src/test/java/com/iluwatar/cachingfilter/CacheStoreTest.java Tests covering empty lookups, expiry behaviour, eviction by path, and invalid TTL handling.
caching-filter/src/test/java/com/iluwatar/cachingfilter/CachingFilterTest.java Tests for caching behavior across hits, misses, expiry, eviction, and non-GET bypass.
caching-filter/src/test/java/com/iluwatar/cachingfilter/FilterChainTest.java Tests for FilterChain ordering, short-circuit behavior, and empty chain handling.
pom.xml Root POM updated to include the caching-filter module in the multi-module build.

autogenerated by presubmit.ai

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ LGTM!

Review Summary

Commits Considered (1)

Add a caching-filter module: a filter in an intercepting-filter chain that serves a stored GET response until it expires, otherwise passes the request on and caches successful responses. Time comes from an injectable InstantSource so expiry is tested without sleeping. Includes README, PlantUML class diagram and unit tests.

Files Processed (18)
  • caching-filter/README.md (1 hunk)
  • caching-filter/etc/caching-filter.urm.png (0 hunks)
  • caching-filter/etc/caching-filter.urm.puml (1 hunk)
  • caching-filter/pom.xml (1 hunk)
  • caching-filter/src/main/java/com/iluwatar/cachingfilter/App.java (1 hunk)
  • caching-filter/src/main/java/com/iluwatar/cachingfilter/CacheStore.java (1 hunk)
  • caching-filter/src/main/java/com/iluwatar/cachingfilter/CachingFilter.java (1 hunk)
  • caching-filter/src/main/java/com/iluwatar/cachingfilter/Filter.java (1 hunk)
  • caching-filter/src/main/java/com/iluwatar/cachingfilter/FilterChain.java (1 hunk)
  • caching-filter/src/main/java/com/iluwatar/cachingfilter/Handler.java (1 hunk)
  • caching-filter/src/main/java/com/iluwatar/cachingfilter/ProductCatalog.java (1 hunk)
  • caching-filter/src/main/java/com/iluwatar/cachingfilter/Request.java (1 hunk)
  • caching-filter/src/main/java/com/iluwatar/cachingfilter/Response.java (1 hunk)
  • caching-filter/src/test/java/com/iluwatar/cachingfilter/AppTest.java (1 hunk)
  • caching-filter/src/test/java/com/iluwatar/cachingfilter/CacheStoreTest.java (1 hunk)
  • caching-filter/src/test/java/com/iluwatar/cachingfilter/CachingFilterTest.java (1 hunk)
  • caching-filter/src/test/java/com/iluwatar/cachingfilter/FilterChainTest.java (1 hunk)
  • pom.xml (1 hunk)
Actionable Comments (0)
Skipped Comments (3)
  • caching-filter/src/main/java/com/iluwatar/cachingfilter/CacheStore.java [67-78]

    maintainability: "Expiry handling is correct and explicitly drops expired entries on lookup."

  • caching-filter/src/main/java/com/iluwatar/cachingfilter/CacheStore.java [91-94]

    maintainability: "removePath evicts all cached entries for a given path (regardless of HTTP method)."

  • caching-filter/src/main/java/com/iluwatar/cachingfilter/CachingFilter.java [60-78]

    maintainability: "Caching behavior looks correct: GETs check the cache first, miss leads to proceeding and potentially caching OK responses. Non-GETs evict the path."

@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.82353% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 84.15%. Comparing base (4cabb20) to head (318828c).

Files with missing lines Patch % Lines
.../src/main/java/com/iluwatar/cachingfilter/App.java 95.23% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3634      +/-   ##
============================================
+ Coverage     84.06%   84.15%   +0.08%     
- Complexity     4353     4382      +29     
============================================
  Files          1133     1140       +7     
  Lines         15400    15485      +85     
  Branches        739      745       +6     
============================================
+ Hits          12946    13031      +85     
+ Misses         2158     2155       -3     
- Partials        296      299       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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.

Caching Filter pattern

1 participant