Call us: +1-415-738-4000
This page provides an example of how to use the included filters to cache web pages and web page fragments.
You'd like to improve the time it takes to return a page from your web application. Many of the pages in your application are not time or user specific and can be cached for a period of time.
Cache the entirety of the web page, or a fragment of the web page for a period of time. Rather than having to generate the page on each page hit, it will be served out of the cache.
Modern application hardware should be able to server as many as 5,000 pages per second, affording a significant speedup in your application for pages that are frequently read but infrequently change.
There are no code changes required for this - your application server should support servlet filtering already. Simply update your web.xml file, re-deploy and you should see the speedup right away.
The basic steps you'll need to follow to configure Ehcache for web page caching are (note that these steps assume you already have Ehcache installed in your application):
The following settings should help you setup web caching for your application.
The first thing you'll need to do is add a filter to enable page caching.
The following web.xml settings will enable a servlet filter for page caching:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " version="2.5"> <filter> <filter-name>SimplePageCachingFilter</filter-name> <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter </filter-class> </filter> <filter-mapping> <filter-name>SimplePageCachingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
The second step to enabling web page caching is to configure ehcache with an appropriate ehcache.xml.
The following ehcache.xml file should configure a reasonable default ehcache:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../main/config/ehcache.xsd"> <cache name="SimplePageCachingFilter" maxEntriesLocalHeap="10000" maxElementsOnDisk="1000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" /> </ehcache>
Now start your application server. Pages should be cached.
For more details and configuration options pertaining to web page and web page fragment caching, see the Web Caching page in the user documentation.