Ehcache SimplePageCachingFilter

Add caching layer on your web application, with out modifying the code.

SimplePageCachingFilter

Ehcache has an out-of-box of solution for this ‘SimplePageCachingFilter’.
SimplePageCachingFilter is caching filter can be use full for html or any other response type
e.g JSON or xml. It uses singleton Ehcache manager to store contents. Cache keys are calculated
using the URI and query string. /exampleData?user=admin&role=admin.

There is another variant of ‘SimplePageCachingFilter’ based on headers ‘SimpleCachingHeadersPageCachingFilter’.
This filter take account headers for cache key. It take three account of headers

  • ETag
  • Last-Modified
  • Expires

Configuration

cacheName
Cache used by filter for storing content

blockingTimeoutMillis
the time, in milliseconds, to wait for the filter chain to return with a response on a cache miss. This is useful to fail fast in the event of an infrastructure failure.

varyHeader
set to true to set Vary:Accept-Encoding in the response when doing Gzip. This header is needed to support HTTP proxies however it is off by default.

Web.xml

<filter>
<filter-name>SimplePageCachingFilter</filter-name>
 <filter-class>net.sf.ehcache.constructs.web.filter.
SimplePageFragmentCachingFilter
 </filter-class>
 <init-param>
  <param-name>cacheName</param-name>
  <param-value>simplePageCache</param-value>
 </init-param>
</filter>
<filter-mapping>
<filter-name>SimplePageCachingFilter</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

Ehcache configuration

<Ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../main/config/ehcache.xsd">
<diskStorepath="java.io.tmpdir"/>
 <defaultCache
   maxEntriesLocalHeap="10"
   eternal="false"
   timeToIdleSeconds="5"
   timeToLiveSeconds="10"
   overflowToDisk="true"
   />
  <!-- Page and Page Fragment Caches -->
<cachename="simplePageCache"
  maxEntriesLocalHeap="10"
  eternal="false"
  timeToIdleSeconds="10000"
  timeToLiveSeconds="10000"
  overflowToDisk="true">
</cache>
</ehcache>

Pros

  • No code is required.
  • It allows the content compressing.
  • This can be integrated with Spring WebFramework too.

Cons

  • It do caching based on complete response.
  • It doesn’t take custom headers in account.

Java bug

I am Java professional, working on Enterprise Java from last 8 years.

More Posts - Website

Follow Me:
TwitterLinkedIn