Using Ehcache

Ehcache can be used directly. It can also be used with the popular Hibernate Object/Relational tool and Java EE Servlet Caching. This quick guide gets you started on each of these. The rest of the documentation can be explored for a deeper understanding.

General-Purpose Caching

  • Download Ehcache ›

    Beginning with Ehcache 1.7.1, Ehcache depends on SLF4J (http://www.slf4j.org) for logging. SLF4J is a logging framework with a choice of concrete logging implementations. See the chapter on Logging for configuration details.

  • Use Java 1.5 or 1.6.

  • Place the Ehcache jar into your classpath.
  • Configure ehcache.xml and place it in your classpath.
  • Optionally, configure an appropriate logging level. See the Code Samples chapter for more information on direct interaction with ehcache.

Hibernate

  • Perform the same steps as for general-purpose caching (above).
  • Create caches in ehcache.xml.

See the Hibernate Caching chapter for more information.

Distributed Caching

Distributed Ehcache combines the power of the Terracotta platform with the ease of Ehcache application-data caching. Ehcache supports distributed caching with two lines of configuration.

By integrating Enterprise Ehcache with the Terracotta platform, you can take advantage of BigMemory and expanded Terracotta Server Arrays to greatly scale your application and cluster.

The distributed-cache documentation covers how to configure Ehcache in a Terracotta cluster and how to use its API in your application.

Java EE Servlet Caching

  • Perform the same steps as for general-purpose caching above.
  • Configure a cache for your web page in ehcache.xml.
  • To cache an entire web page, either use SimplePageCachingFilter or create your own subclass of CachingFilter
  • To cache a jsp:Include or anything callable from a RequestDispatcher, either use SimplePageFragmentCachingFilter or create a subclass of PageFragmentCachingFilter.
  • Configure the web.xml. Declare the filters created above and create filter mapping associating the filter with a URL.

See the Web Caching chapter for more information.

RESTful and SOAP Caching with the Cache Server

  • Download the ehcache-standalone-server from https://sourceforge.net/projects/ehcache/files/ehcache-server.
  • cd to the bin directory
  • Type startup.sh to start the server with the log in the foreground. By default it will listen on port 8080, will have both RESTful and SOAP web services enabled, and will use a sample Ehcache configuration from the WAR module.
  • See the code samples in the Cache Server chapter. You can use Java or any other programming language to the use the Cache Server.

See the Cache Server chapter for more information.

JCache style caching

Ehcache contains an early draft implementation of JCache contained in the net.sf.ehcache.jcache package. See the JSR107 chapter for usage.

Spring, Cocoon, Acegi and other frameworks

Usually, with these, you are using Ehcache without even realising it. The first steps in getting more control over what is happening are:

  • discover the cache names used by the framework
  • create your own ehcache.xml with settings for the caches and place it in the application classpath.
Feedback