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.

Interfaces in Hibernate

There are 5 main Interfaces in Hibernate which form its lifeline. All of them are found in the org.hibernate package. An overview of the hibernate APIs can be found at www.hibernate.org

• Configuration interface

o Used to configure hibernate in any java application
o The java application is provided with a JDBC connection and resource mapping using the Configuration API
o It’s the first object a user uses while working with hibernate

• SessionFactory interface

o This interface is used to obtain session objects
o Though it is thread safe, we need one SessionFactory for each database that the application is connecting to.
o It acts as a kind of Cache storage during runtime giving the database sessions available for java objects.

• Session interface

o This is the primary Interface used by hibernate
o It is known as the hibernate’s persistence manager
o It is the interface used to obtain a transaction.
o It is different from the httpSession object and the two should not be confused.
o Session objects are not thread safe

• Transaction interface

o Though an optional API, it performs the all important task of abstracting the application from the underlying complex JDBC or JTA transaction.
o Each transaction object represents an atomic unit of work.
o Each session may have one or more transactions.

• Criteria and Query interface

o Query interface controls the “how” and “what” of executing queries on a database.
o Queries can be written in native SQL or in hibernate query language
o Criteria queries are those created using Java objects and Criteria interface is useful in helping to create them.