Ehcache Architecture, Features and
Usage Patterns
Greg Luck
Maintainer, ehcache - http://ehcache.sf.net
Member, JSR 107 JC...
Agenda
Introduction
The Theory of Caching
Ehcache Architecture
General Purpose Caching
Web Caching
Java Persistence API (J...
Introduction
About Ehcache
    Since 2003
●


    The most widely used Java platform Cache
●


    Apache 2.0 License
●


...
The Theory of Caching
How much faster will caching make an application?
It depends on a multitude of factors being:
● how ...
The Theory of Caching
Cache Efficiency
Factors which affect the efficiency of a cache are:
● Required Liveness of Data


●...
The Theory of Caching
How to calculate entire system speedup: Amdahl's Law
Amdahl's law, after Gene Amdahl, is used to fin...
The Theory of Caching
Speed up from a Web Page Cache
Uncached page time: 2 seconds
Cache retrieval time: 2ms
Proportion: 1...
The Theory of Caching
Speed up from a Database Level Cache
Uncached page time: 2 seconds
Database time: 1.5 seconds
Cache ...
Architecture




               2009 Updated from JavaOne Session 2007 |   9
General Purpose Caching
Step by Step
1. Add ehcache Java Archive (JAR) to your
   classpath
2. Place configuration in ehca...
Usage - General Purpose Caching
ehcache.xml Configuration
<ehcache>

   <diskStore path=quot;java.io.tmpdirquot;/>

   ......
Web Caching
Step by Step
1. Use or Subclass SimpleCachingFilter or
   SimplePageFragmentCachingFilter
2. Configure filter ...
Web Caching
Example Scenario



  /index.jsp




 /include/footer.jsp




                       2009 Updated from JavaOne...
Filter Configuration in web.xml
Full Page Cache
<filter>
   <filter-name>SimplePageCachingFilter</filter-name>
   <filter-...
Add Cache to Ehcache Configuration
Full Page Cache
<ehcache>

   <diskStore path=quot;java.io.tmpdirquot;/>

   ...

   <c...
Filter Configuration in web.xml
Page Fragment Cache
<filter>
   <filter-name>SimplePageFragmentCache</filter-name>
   <fil...
JPA/Hibernate Caching
Overview
    Hibernate can either be used directly either or
●

    via the Hibernate Java Persisten...
JPA/Hibernate Caching
Step by Step
1. Configure Hibernate to use ehcache
2. Configure the Country object's cacheability. T...
JPA/Hibernate Caching
Configure Hibernate to use ehcache


Add the following to hibernate.properties
hibernate.cache.provi...
JPA/Hibernate Caching
Native Hibernate Mapping File Configuration
<hibernate-mapping>

<class
    name=quot;com.somecompan...
JPA/Hibernate Caching
Entity Configuration with JPA and Hibernate Annotations

@Entity
...
@Cache(usage = CacheConcurrency...
Distributed Caching for Clusters
Features
1. Pluggable distribution mechanism.
2. RMI, JGroups and JMS replication modules...
Distributed Caching for Clusters
Architecture




                 2009 Updated from JavaOne Session 2007 |   23
Distributed Caching for Clusters
RMI Based Replication
1. Multicast Peer Discovery
  <cacheManagerPeerProviderFactory clas...
Distributed Caching for Clusters
    Set Replication Per Cache
<cache ...>
   <cacheEventListenerFactory
  class=quot;net....
Distributed Caching for Clusters
    Set Bootstrapping Per Cache
<cache ...>
       <bootstrapCacheLoaderFactory class=quo...
Cache Server
REST and SOAP APIs
Supports HTTP/1.1 request pipelining
Deployed to a WAR or self-contained with Glassfish
Em...
Cache Server
Architecture




               2009 Updated from JavaOne Session 2007 |   28
Cache Server
 Ehcache in-process compared with Memcached
 (Ehcache server would be similar to memcached)
7000

6000

5000
...
Cache Server
Client coding examples
Scala
import java.net.URL
   import scala.io.Source.fromInputStream

   object Example...
Summary
    Most apps will benefit from caching
●


    You can calculate the speed up
●


    Ehcache is a modern, modula...
For More Information
    Project Website:
●

    http://ehcache.sf.net
    Downloadable Book:
●


    http://www.lulu.com/...
Q&A
Greg Luck
gluck@gregluck.com




                                                                33
                  ...
Upcoming SlideShare
Loading in...5
×

Ehcache Architecture, Features And Usage Patterns

13,976

Published on

In depth presentation on Ehcache from Greg Luck, the Wotif.com Chief Architect and Ehcache maintainer

Published in: Technology
0 Comments
17 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
13,976
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
602
Comments
0
Likes
17
Embeds 0
No embeds

No notes for slide

Ehcache Architecture, Features And Usage Patterns

  1. 1. Ehcache Architecture, Features and Usage Patterns Greg Luck Maintainer, ehcache - http://ehcache.sf.net Member, JSR 107 JCACHE Expert Group Chief Architect, Wotif.com - http://wotif.com 2009 Updated from JavaOne Session 2007 |
  2. 2. Agenda Introduction The Theory of Caching Ehcache Architecture General Purpose Caching Web Caching Java Persistence API (JPA)/Hibernate Caching Distributed Caching for Clusters Cache Server Step-by-Step Coding Demo 2009 Updated from JavaOne Session 2007 | 2
  3. 3. Introduction About Ehcache Since 2003 ● The most widely used Java platform Cache ● Apache 2.0 License ● Integrated by lots of projects, products ● Hibernate Provider implemented 2003 ● Web Caching 2004 ● Distributed Caching 2006 ● JCACHE implementation 2007 ● Cache Server 2008 ● 2009 Updated from JavaOne Session 2007 | 3
  4. 4. The Theory of Caching How much faster will caching make an application? It depends on a multitude of factors being: ● how many times a cached piece of data can and is reused by the application ● the proportion of the response time that is alleviated by caching ● In applications that are I/O bound, which is most business applications, most of the response time is getting data from a database. Therefore the speed up mostly depends on how much reuse a piece of data gets. 2009 Updated from JavaOne Session 2007 | 4
  5. 5. The Theory of Caching Cache Efficiency Factors which affect the efficiency of a cache are: ● Required Liveness of Data ● Proportion of total data cached ● Read/Write ratio ● Caching in a single VM versus Caching Clusters 2009 Updated from JavaOne Session 2007 | 5
  6. 6. The Theory of Caching How to calculate entire system speedup: Amdahl's Law Amdahl's law, after Gene Amdahl, is used to find the system speed up from a speed up in part of the system. 1/ ((1 - Proportion Sped Up) + Proportion Sped Up / Speed up) Things to Watch: ● For a web application the “system” should include browser render time and network latency. 2009 Updated from JavaOne Session 2007 | 6
  7. 7. The Theory of Caching Speed up from a Web Page Cache Uncached page time: 2 seconds Cache retrieval time: 2ms Proportion: 100% The expected server side system speedup is thus: 1 / ((1 - 1) + 1 / 1000) = 1 / (0 + .001) = 1000 times system speedup The to the browser “system” speed up is much less 2009 Updated from JavaOne Session 2007 | 7
  8. 8. The Theory of Caching Speed up from a Database Level Cache Uncached page time: 2 seconds Database time: 1.5 seconds Cache retrieval time: 2ms Proportion: 75% (1.5/2) The expected system speedup is thus: 1 / ((1 - .75) + .75 / (1500/2))) = 1 / (.25 + .75/750) = 3.98 times system speedup 2009 Updated from JavaOne Session 2007 | 8
  9. 9. Architecture 2009 Updated from JavaOne Session 2007 | 9
  10. 10. General Purpose Caching Step by Step 1. Add ehcache Java Archive (JAR) to your classpath 2. Place configuration in ehcache.xml and add it to your classpath 3. Create a CacheManager CacheManager manager = CacheManager.create(); 4. Reference a Cache Ehcache sampleCache = manager.getCache(); 5. Use it sampleCache.put(new Element(“key”, “value”)); sampleCache.get(“key”); 2009 Updated from JavaOne Session 2007 | 10
  11. 11. Usage - General Purpose Caching ehcache.xml Configuration <ehcache> <diskStore path=quot;java.io.tmpdirquot;/> ... <cache name=quot;sampleCache1quot; maxElementsInMemory=quot;10000quot; maxElementsOnDisk=quot;1000quot; eternal=quot;falsequot; overflowToDisk=quot;truequot; timeToIdleSeconds=quot;300quot; timeToLiveSeconds=quot;600quot; memoryStoreEvictionPolicy=quot;LFUquot; /> </ehcache> 2009 Updated from JavaOne Session 2007 | 11
  12. 12. Web Caching Step by Step 1. Use or Subclass SimpleCachingFilter or SimplePageFragmentCachingFilter 2. Configure filter in web.xml 3. Create a mapping in web.xml 4. Configure a cache entry matching the filter name 2009 Updated from JavaOne Session 2007 | 12
  13. 13. Web Caching Example Scenario /index.jsp /include/footer.jsp 2009 Updated from JavaOne Session 2007 | 13
  14. 14. Filter Configuration in web.xml Full Page Cache <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>/index.jsp</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>INCLUDE</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> 2009 Updated from JavaOne Session 2007 | 14
  15. 15. Add Cache to Ehcache Configuration Full Page Cache <ehcache> <diskStore path=quot;java.io.tmpdirquot;/> ... <cache name=quot;SimplePageCachingFilterquot; maxElementsInMemory=quot;10000quot; maxElementsOnDisk=quot;1000quot; eternal=quot;falsequot; overflowToDisk=quot;truequot; timeToIdleSeconds=quot;300quot; timeToLiveSeconds=quot;600quot; memoryStoreEvictionPolicy=quot;LFUquot; /> </ehcache> 2009 Updated from JavaOne Session 2007 | 15
  16. 16. Filter Configuration in web.xml Page Fragment Cache <filter> <filter-name>SimplePageFragmentCache</filter-name> <filter-class>net.sf.ehcache.constructs.web.filter. SimplePageFragmentCachingFilter </filter-class> </filter> <!-- Page Fragment Cache --> <filter-mapping> <filter-name>SimplePageFragmentCachingFilter </filter-name> <url-pattern>/include/Footer.jsp</url-pattern> </filter-mapping> 2009 Updated from JavaOne Session 2007 | 16
  17. 17. JPA/Hibernate Caching Overview Hibernate can either be used directly either or ● via the Hibernate Java Persistence API Provider in Hibernate 3.2 Ehcache is a CacheProvider for Hibernate 2.x ● and 3.x Example Simple Country persistent ● object/Entity 2009 Updated from JavaOne Session 2007 | 17
  18. 18. JPA/Hibernate Caching Step by Step 1. Configure Hibernate to use ehcache 2. Configure the Country object's cacheability. This can be done in a number of ways. 3. Decide whether to use defaults, or to configure specific cache settings for the Country object cache in ehcache.xml 2009 Updated from JavaOne Session 2007 | 18
  19. 19. JPA/Hibernate Caching Configure Hibernate to use ehcache Add the following to hibernate.properties hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider <!-- optional configuration file parameter --> net.sf.ehcache.configurationResourceName=/name_of_configuration_resource 2009 Updated from JavaOne Session 2007 | 19
  20. 20. JPA/Hibernate Caching Native Hibernate Mapping File Configuration <hibernate-mapping> <class name=quot;com.somecompany.someproject.domain.Countryquot; table=quot;ut_Countriesquot; dynamic-update=quot;falsequot; dynamic-insert=quot;falsequot; > <cache usage=quot;read-write|nonstrict-read-write|read-onlyquot; /> </class> ... </hibernate-mapping> <cache usage=quot;transactional|read-write|nonstrict-read-write|read-onlyquot; region=quot;RegionNamequot; include=quot;all|non-lazyquot; /> 2009 Updated from JavaOne Session 2007 | 20
  21. 21. JPA/Hibernate Caching Entity Configuration with JPA and Hibernate Annotations @Entity ... @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public Class Country { private String name; ... } 2009 Updated from JavaOne Session 2007 | 21
  22. 22. Distributed Caching for Clusters Features 1. Pluggable distribution mechanism. 2. RMI, JGroups and JMS replication modules. Terracotta also has an ehcache provider. 3. Multiple “clusters” 4. Replication set per Cache. Options: 1. Replicate adds 2. Replicate removes 3. Replicate update by copy or invalidate 4. Async or Sync 5. Bootstrapping from existing Caches in a cluster 2009 Updated from JavaOne Session 2007 | 22
  23. 23. Distributed Caching for Clusters Architecture 2009 Updated from JavaOne Session 2007 | 23
  24. 24. Distributed Caching for Clusters RMI Based Replication 1. Multicast Peer Discovery <cacheManagerPeerProviderFactory class= quot;net.sf.ehcache.distribution. RMICacheManagerPeerProviderFactoryquot; properties=quot;peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446/> 2. RMI Listener <cacheManagerPeerListenerFactory class= quot;net.sf.ehcache.distribution.RMICacheManagerPeerLis tenerFactoryquot;properties=quot;port=40001quot;/> 2009 Updated from JavaOne Session 2007 | 24
  25. 25. Distributed Caching for Clusters Set Replication Per Cache <cache ...> <cacheEventListenerFactory class=quot;net.sf.ehcache.distribution.RMICacheReplicatorFactoryquot; properties=quot;replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true, asynchronousReplicationIntervalMillis=1000quot;/> ... </cache> 2009 Updated from JavaOne Session 2007 | 25
  26. 26. Distributed Caching for Clusters Set Bootstrapping Per Cache <cache ...> <bootstrapCacheLoaderFactory class=quot; net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactoryquot; properties=quot;bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000quot;/> </cache> 2009 Updated from JavaOne Session 2007 | 26
  27. 27. Cache Server REST and SOAP APIs Supports HTTP/1.1 request pipelining Deployed to a WAR or self-contained with Glassfish Embedded Benefits over memcached: •clients in any language •may utilise HTTP load balancers •servers are clustered using ehcache replication 2009 Updated from JavaOne Session 2007 | 27
  28. 28. Cache Server Architecture 2009 Updated from JavaOne Session 2007 | 28
  29. 29. Cache Server Ehcache in-process compared with Memcached (Ehcache server would be similar to memcached) 7000 6000 5000 ehcache-1.2.4 memory 4000 ehcache-1.2.4 disk memcached-1.2.1 3000 2000 1000 0 put/set get remove/delete 2009 Updated from JavaOne Session 2007 | 29
  30. 30. Cache Server Client coding examples Scala import java.net.URL import scala.io.Source.fromInputStream object ExampleScalaGet extends Application { val url = new URL(quot;http://localhost:8080/ehcache/rest/sampleCache2/2quot;) fromInputStream(url.openStream).getLines.foreach(print) } PHP <?php $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, quot;http://localhost:8080/ehcache/rest/sampleCache2/3quot;); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_exec ($ch); curl_close ($ch); ?> 2009 Updated from JavaOne Session 2007 | 30
  31. 31. Summary Most apps will benefit from caching ● You can calculate the speed up ● Ehcache is a modern, modular family of caching ● tools You can also benefit from ehcache indirectly ● through frameworks that use it Distributed ehcache is a small configuration step ● Cache Server is the new kid on the block ● 2009 Updated from JavaOne Session 2007 | 31
  32. 32. For More Information Project Website: ● http://ehcache.sf.net Downloadable Book: ● http://www.lulu.com/content/paperback- ● book/ehcache-150-guide-reference/3553390 My Email: ● gluck@gregluck.com 2009 Updated from JavaOne Session 2007 | 32
  33. 33. Q&A Greg Luck gluck@gregluck.com 33 2009 Updated from JavaOne Session 2007 |
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×