Top Banner
2009 Updated from JavaOne Session 2007 | 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
33

Ehcache Architecture, Features And Usage Patterns

May 06, 2015

Download

Technology

In depth presentation on Ehcache from Greg Luck, the Wotif.com Chief Architect and Ehcache maintainer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 |

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

Page 2: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 2

Agenda

The Theory of CachingEhcache ArchitectureGeneral Purpose CachingWeb Caching Java Persistence API (JPA)/Hibernate CachingDistributed Caching for ClustersCache ServerStep-by-Step Coding Demo

Introduction

Page 3: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 3

Introduction

● 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

About Ehcache

Page 4: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 4

How much faster will caching make an application?

The Theory of Caching

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.

Page 5: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 5

Cache EfficiencyThe Theory of Caching

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

Page 6: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 6

How to calculate entire system speedup: Amdahl's LawThe Theory of Caching

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.

Page 7: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 7

Speed up from a Web Page CacheThe Theory of Caching

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

Page 8: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 8

Speed up from a Database Level CacheThe Theory of Caching

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

Page 9: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 9

Architecture

Page 10: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 10

General Purpose Caching

1.Add ehcache Java Archive (JAR) to your classpath

2.Place configuration in ehcache.xml and add it to your classpath

3.Create a CacheManagerCacheManager manager = CacheManager.create();

4.Reference a CacheEhcache sampleCache = manager.getCache();

5.Use itsampleCache.put(new Element(“key”, “value”));sampleCache.get(“key”);

Step by Step

Page 11: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 11

Usage - General Purpose Caching

<ehcache>

<diskStore path="java.io.tmpdir"/>

...

<cache name="sampleCache1" maxElementsInMemory="10000" maxElementsOnDisk="1000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" /></ehcache>

ehcache.xml Configuration

Page 12: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 12

Step by StepWeb Caching

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

Page 13: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 13

Example ScenarioWeb Caching

/index.jsp

/include/footer.jsp

Page 14: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 14

Filter Configuration in web.xml

<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>

Full Page Cache

Page 15: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 15

Add Cache to Ehcache Configuration

<ehcache>

<diskStore path="java.io.tmpdir"/>

...

<cache name="SimplePageCachingFilter" maxElementsInMemory="10000" maxElementsOnDisk="1000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LFU" /></ehcache>

Full Page Cache

Page 16: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 16

Filter Configuration in web.xml

<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>

Page Fragment Cache

Page 17: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 17

OverviewJPA/Hibernate Caching

● 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

Page 18: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 18

Step by StepJPA/Hibernate Caching

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

Page 19: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 19

Configure Hibernate to use ehcacheJPA/Hibernate Caching

hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider

<!-- optional configuration file parameter -->net.sf.ehcache.configurationResourceName=/name_of_configuration_resource

Add the following to hibernate.properties

Page 20: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 20

Native Hibernate Mapping File ConfigurationJPA/Hibernate Caching

<cache usage="transactional|read-write|nonstrict-read-write|read-only" region="RegionName" include="all|non-lazy" />

<hibernate-mapping>

<class name="com.somecompany.someproject.domain.Country" table="ut_Countries" dynamic-update="false" dynamic-insert="false">

<cache usage="read-write|nonstrict-read-write|read-only" /></class>...</hibernate-mapping>

Page 21: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 21

Entity Configuration with JPA and Hibernate AnnotationsJPA/Hibernate Caching

@Entity...@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)public Class Country {

private String name;

...

}

Page 22: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 22

FeaturesDistributed Caching for Clusters

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 adds2.Replicate removes3.Replicate update by copy or invalidate4.Async or Sync

5.Bootstrapping from existing Caches in a cluster

Page 23: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 23

Distributed Caching for ClustersArchitecture

Page 24: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 24

RMI Based Replication Distributed Caching for Clusters

1.Multicast Peer Discovery<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446/>

2. RMI Listener<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"properties="port=40001"/>

Page 25: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 25

Set Replication Per CacheDistributed Caching for Clusters

<cache ...><cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=true, replicatePuts=true,

replicateUpdates=true,replicateUpdatesViaCopy=true,replicateRemovals=true,asynchronousReplicationIntervalMillis=1000"/>...

</cache>

Page 26: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 26

Set Bootstrapping Per CacheDistributed Caching for Clusters

<cache ...><bootstrapCacheLoaderFactory class="

net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000"/>

</cache>

Page 27: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 27

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

Cache Server

Page 28: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 28

ArchitectureCache Server

Page 29: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 29

Ehcache in-process compared with Memcached(Ehcache server would be similar to memcached)

Cache Server

put/set get remove/delete0

1000

2000

3000

4000

5000

6000

7000

ehcache-1.2.4 memoryehcache-1.2.4 diskmemcached-1.2.1

Page 30: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 30

Client coding examplesCache Server

import java.net.URL

import scala.io.Source.fromInputStream

object ExampleScalaGet extends Application {

val url = new URL("http://localhost:8080/ehcache/rest/sampleCache2/2")

fromInputStream(url.openStream).getLines.foreach(print) }

Scala

PHP<?php

$ch = curl_init();

curl_setopt ($ch, CURLOPT_URL, "http://localhost:8080/ehcache/rest/sampleCache2/3");

curl_setopt ($ch, CURLOPT_HEADER, 0);

curl_exec ($ch);

curl_close ($ch); ?>

Page 31: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 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

Page 32: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 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:

[email protected]

Page 33: Ehcache Architecture, Features And Usage Patterns

2009 Updated from JavaOne Session 2007 | 33

Q&AGreg [email protected]