Name

Caching Filter

Goal

Minimize repeated page generation by caching dynamic pages when they are generated (Figure A-13).

Classes in the Caching Filter pattern
Figure A-13. Classes in the Caching Filter pattern

Participants

Filter

A common interface implemented by the cache, decorators and target.

Cache filter

Intercepts all requests and replies efficiently with cached pages if available. If no cached data is available, caches the results of generating the page.

Decorators

Encapsulate a piece of common functionality, while presenting the same interface as the target.

Target

The final object in the request processing chain. Coordinates all specific activities, such as input handling. Typically the front controller (Figure A-14).

Interactions in the Caching Filter pattern
Figure A-14. Interactions in the Caching Filter pattern

Interactions

The cache filter intercepts all requests destined for target and determines if the page has been cached. If it is cached, data is returned from the cache. If not, the remaining decorators in the chain and the target are executed to generate the page, and the results are cached.

Notes

The caching filter is a variation of the decorator pattern. To ease implementation of filters such as the caching filter, the servlet API allows you to decorate the HTTPServletResponse object in order to store the results of request processing.

Get J2EE Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.