<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tellaw.org &#187; cache</title>
	<atom:link href="http://www.tellaw.org/tag/cache/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tellaw.org</link>
	<description>La technologie doit devenir simple...</description>
	<lastBuildDate>Fri, 02 Apr 2010 14:48:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Gerer un cache avec EhCache.</title>
		<link>http://www.tellaw.org/2008/04/25/gerer-un-cache-avec-ehcache/</link>
		<comments>http://www.tellaw.org/2008/04/25/gerer-un-cache-avec-ehcache/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 09:58:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[ehcache]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.tellaw.org/?p=47</guid>
		<description><![CDATA[La gestion d&#8217;un cache applicatif est souvent une question demandant pas mal de ressources pour être résolue. EhCache est une solution simple et rapide à mettre en oeuvre, capable de s&#8217;interfacer facilement avec vos applications, et même le système de logs de votre application&#160;!
Comme à mon habtude, voici n extrait du site de ehcache définissant [...]]]></description>
			<content:encoded><![CDATA[<p>La gestion d&#8217;un cache applicatif est souvent une question demandant pas mal de ressources pour être résolue. EhCache est une solution simple et rapide à mettre en oeuvre, capable de s&#8217;interfacer facilement avec vos applications, et même le système de logs de votre application&nbsp;!<br/><br />
Comme à mon habtude, voici n extrait du site de ehcache définissant ce produit&nbsp;:<br/><br />
<i>&laquo;&nbsp;Ehcache is a pure Java, in-process cache&nbsp;&raquo;</i>, <i>&laquo;&nbsp;Available under the Apache 1.1 license. Ehcache&#8217;s copyright and licensing has been reviewed and approved by the Apache Software Foundation, making ehcache suitable for use in Apache projects.&nbsp;&raquo;</i></p>
<p><span id="more-47"></span></p>
<p><strong>1) Les dépendances :</strong></p>
<ul>
<li>Le Jar de EhCache, actuellement&nbsp;: ehcache-1.1.jar.</li>
<li>Le jar de log4j, actuellement&nbsp;: log4j-1.2.9.jar.</li>
<li>Jakarta Commons Collections&nbsp;: <a href="http://jakarta.apache.org/commons/collections/" hreflang="fr">http://jakarta.apache.org/commons/collections/</a>.</li>
<li>Jakarta Commons Loggins&nbsp;: <a href="http://jakarta.apache.org/commons/logging/" hreflang="fr">http://jakarta.apache.org/commons/logging/</a></li>
</ul>
<p><strong>2) Configurer le système de logs (pour mon exemple log4j) :</strong></p>
<p>
<i>Fichier : WEB-INF/log4j.properties</i></p>

<div class="wp_syntax"><div class="code"><pre class="ini"># Set root logger level to DEBUG and its only appender to A1.
log4j.<span style="color: #000099;">rootLogger</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">DEBUG, A1</span>
&nbsp;
# A1 is set to be a ConsoleAppender.
log4j.appender.<span style="color: #000099;">A1</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">org.apache.log4j.ConsoleAppender</span>
&nbsp;
# A1 uses PatternLayout.
log4j.appender.A1.<span style="color: #000099;">layout</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">org.apache.log4j.PatternLayout</span>
log4j.appender.A1.layout.<span style="color: #000099;">ConversionPattern</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">%-4r <span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>%t<span style="">&#93;</span></span> %-5p %c %x - %m%n</span></pre></div></div>

<p>Notez que pour le passage en production de votre application, il faut remonter le niveau d&#8217;alerte de log4j, sinon le serveur va logguer les messages de débug.</p>
<p><strong>3) Création d&#8217;un objet serializable à mettre dans le cache :</strong></p>
<p>
<i>Fichier : Movie.java</i></p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #808080; font-style: italic;">/*
 * Created on 22 avr. 2005
 *
 */</span>
 <span style="color: #000000; font-weight: bold;">package</span> com.<span style="color: #006600;">tellaw</span>.<span style="color: #006600;">ehCacheSample</span><span style="color: #66cc66;">;</span>
&nbsp;
 <span style="color: #a1a100;">import java.io.Serializable;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">/**
 * @author Eric
 *
 */</span>
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Movie <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #aaaadd; font-weight: bold;">Serializable</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #aaaadd; font-weight: bold;">String</span> name = <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #aaaadd; font-weight: bold;">String</span> description = <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #aaaadd; font-weight: bold;">String</span> actor = <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Movie <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// Constructeur par défaut de movie.	</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #aaaadd; font-weight: bold;">String</span> getActor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> actor<span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setActor<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> actor<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">actor</span> = actor<span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #aaaadd; font-weight: bold;">String</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> description<span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setDescription<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> description<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">description</span> = description<span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #aaaadd; font-weight: bold;">String</span> getName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setName<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">name</span> = name<span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

</p>
<p><strong>4) Configuration de EhCache par un fichier de conf externe :</strong></p>
<p><i>Fichier : WEB-INF/ehCache.xml</i></p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ehcache<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- 
		Sets the path to the directory where cache .data files are created.
&nbsp;
		If the path is a Java System Property it is replaced by
		its value in the running VM.
&nbsp;
		The following properties are translated:
		user.home - User's home directory
		user.dir - User's current working directory
		java.io.tmpdir - Default temp file path 
	--&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;diskStore</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;java.io.tmpdir&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--
&nbsp;
		Default Cache configuration. 
&nbsp;
		These will applied to caches programmatically created through the CacheManager.
&nbsp;
		The following attributes are required:        
		maxInMemory                    - Sets the maximum number of objects that will be created in memo
		eternal                        - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element is never expired.
		overflowToDisk                 - Sets whether elements can overflow to disk when the in-memory cache has reached the maxInMemory limit.
&nbsp;
		The following attributes are optional:
		timeToIdleSeconds              - Sets the time to idle for an element before it expires.  i.e. The maximum amount of time between accesses before an element expires Is only used if the element is not eternal.
		Optional attribute. A value of 0 means that an Element can idle for infinity.
		The default value is 0.
		timeToLiveSeconds              - Sets the time to live for an element before it expires.
		i.e. The maximum time between creation time and when an element expires.
		Is only used if the element is not eternal.
		Optional attribute. A value of 0 means that and Element can live for infinity.
		The default value is 0.
&nbsp;
		diskPersistent                 - Whether the disk store persists between restarts of the Virtual Machine.
&nbsp;
		The default value is false.
		diskExpiryThreadIntervalSeconds- The number of seconds between runs of the disk expiry thread. 
&nbsp;
		The default value
		is 120 seconds.
&nbsp;
	--&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;defaultCache</span>
&nbsp;
		<span style="color: #000066;">maxElementsInMemory</span>=<span style="color: #ff0000;">&quot;10000&quot;</span>
		<span style="color: #000066;">eternal</span>=<span style="color: #ff0000;">&quot;false&quot;</span>
		<span style="color: #000066;">timeToIdleSeconds</span>=<span style="color: #ff0000;">&quot;120&quot;</span>
		<span style="color: #000066;">timeToLiveSeconds</span>=<span style="color: #ff0000;">&quot;120&quot;</span>
		<span style="color: #000066;">overflowToDisk</span>=<span style="color: #ff0000;">&quot;true&quot;</span>
		<span style="color: #000066;">diskPersistent</span>=<span style="color: #ff0000;">&quot;false&quot;</span>
		<span style="color: #000066;">diskExpiryThreadIntervalSeconds</span>=<span style="color: #ff0000;">&quot;120&quot;</span>
&nbsp;
	<span style="font-weight: bold; color: black;">/&gt;</span></span>    
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!--Predefined caches.  
		Add your cache configuration settings here.
&nbsp;
		If you do not have a configuration for your cache a WARNING will be issued when the
		CacheManager starts
&nbsp;
		The following attributes are required:
		name                           - Sets the name of the cache. This is used to identify the cache.
		It must be unique.
&nbsp;
		maxInMemory                    - Sets the maximum number of objects that will be created in memory
		eternal                        - Sets whether elements are eternal. If eternal,  timeouts are ignored and the
		element is never expired.
&nbsp;
		overflowToDisk                 - Sets whether elements can overflow to disk when the in-memory cache
		has reached the maxInMemory limit.
&nbsp;
		The following attributes are optional:
		timeToIdleSeconds              - Sets the time to idle for an element before it expires.
		i.e. The maximum amount of time between accesses before an element expires
		Is only used if the element is not eternal.
		Optional attribute. A value of 0 means that an Element can idle for infinity.
&nbsp;
		The default value is 0.
&nbsp;
		timeToLiveSeconds              - Sets the time to live for an element before it expires.
		i.e. The maximum time between creation time and when an element expires.
		Is only used if the element is not eternal.
		Optional attribute. A value of 0 means that and Element can live for infinity.
		The default value is 0.
&nbsp;
		diskPersistent                 - Whether the disk store persists between restarts of the Virtual Machine.
		The default value is false.
&nbsp;
		diskExpiryThreadIntervalSeconds- The number of seconds between runs of the disk expiry thread. The default value
		is 120 seconds.
	--&gt;</span></span>
&nbsp;
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- 
		Sample cache named sampleCache1
&nbsp;
		This cache contains a maximum in memory of 10000 elements, and will expire
		an element if it is idle for more than 5 minutes and lives for more than
		10 minutes.
&nbsp;
		If there are more than 10000 elements it will overflow to the
		disk cache, which in this configuration will go to wherever java.io.tmp is
		defined on your system. On a standard Linux system this will be /tmp&quot;
	--&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;cache</span> 	<span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sampleCache1&quot;</span>
			<span style="color: #000066;">maxElementsInMemory</span>=<span style="color: #ff0000;">&quot;10000&quot;</span>
			<span style="color: #000066;">eternal</span>=<span style="color: #ff0000;">&quot;false&quot;</span>
			<span style="color: #000066;">overflowToDisk</span>=<span style="color: #ff0000;">&quot;false&quot;</span>
			<span style="color: #000066;">timeToIdleSeconds</span>=<span style="color: #ff0000;">&quot;300&quot;</span>
			<span style="color: #000066;">timeToLiveSeconds</span>=<span style="color: #ff0000;">&quot;600&quot;</span>
	<span style="font-weight: bold; color: black;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ehcache<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

</p>
<p><strong>5) Création d&#8217;un servlet utilisant EhCache pour mémoriser un objet :</strong></p>
<p>
<i>Fichier : TestServlet.java</i></p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #808080; font-style: italic;">/*
 * Created on 22 avr. 2005
 *
 */</span>
 <span style="color: #000000; font-weight: bold;">package</span> com.<span style="color: #006600;">tellaw</span>.<span style="color: #006600;">ehCacheSample</span>.<span style="color: #006600;">servlets</span><span style="color: #66cc66;">;</span>
&nbsp;
 <span style="color: #a1a100;">import java.io.IOException;</span>
 <span style="color: #a1a100;">import java.io.PrintWriter;</span>
&nbsp;
 <span style="color: #a1a100;">import javax.servlet.ServletException;</span>
 <span style="color: #a1a100;">import javax.servlet.http.HttpServlet;</span>
 <span style="color: #a1a100;">import javax.servlet.http.HttpServletRequest;</span>
 <span style="color: #a1a100;">import javax.servlet.http.HttpServletResponse;</span>
 <span style="color: #a1a100;">import org.apache.log4j.Logger;</span>
 <span style="color: #a1a100;">import org.apache.log4j.PropertyConfigurator;</span>
 <span style="color: #a1a100;">import net.sf.ehcache.*;</span>
&nbsp;
 <span style="color: #a1a100;">import com.tellaw.ehCacheSample.*;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">/**
 * @author Eric
 *
 */</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Logger logger = Logger.<span style="color: #006600;">getLogger</span><span style="color: #66cc66;">&#40;</span>TestServlet.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> CacheManager manager = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Cache cache = <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Configuration de log4J.</span>
		PropertyConfigurator.<span style="color: #006600;">configure</span><span style="color: #66cc66;">&#40;</span> getServletContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getRealPath</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;WEB-INF/log4j.properties&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Creating the cache manager from configuration file</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
			manager = CacheManager.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span>getServletContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getRealPath</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;WEB-INF/ehCache.xml&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			cache = manager.<span style="color: #006600;">getCache</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;sampleCache1&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Exception</span> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			logger.<span style="color: #006600;">error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Unable to load EHCACHE configuration file&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> destroy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> doPost<span style="color: #66cc66;">&#40;</span>HttpServletRequest arg0, HttpServletResponse arg1<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #aaaadd; font-weight: bold;">IOException</span> <span style="color: #66cc66;">&#123;</span>
		doGet<span style="color: #66cc66;">&#40;</span>arg0, arg1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> doGet<span style="color: #66cc66;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #aaaadd; font-weight: bold;">IOException</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Creation du writer de sortie.		</span>
		<span style="color: #aaaadd; font-weight: bold;">PrintWriter</span> out = response.<span style="color: #006600;">getWriter</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Creation d'un objet Movie pour tester notre cache</span>
		Movie myMovie = <span style="color: #000000; font-weight: bold;">new</span> Movie <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		myMovie.<span style="color: #006600;">setName</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The Ring&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		myMovie.<span style="color: #006600;">setDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Film fantastique&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		myMovie.<span style="color: #006600;">setActor</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Naomi Watts&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Mise en cache de cet element</span>
		<span style="color: #808080; font-style: italic;">// Attention ce que la classe Movie implémente java.io.Serializable</span>
		<span style="color: #aaaadd; font-weight: bold;">Element</span> element = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">Element</span><span style="color: #66cc66;">&#40;</span>myMovie.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, myMovie<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		cache.<span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span>element<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		out.<span style="color: #006600;">println</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Objet mis en cache&lt;br/&gt;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Lecture du cache pour extraire l'objet</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
			element = cache.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The Ring&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Exception</span> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
		Movie mySecondMovie = <span style="color: #66cc66;">&#40;</span>Movie<span style="color: #66cc66;">&#41;</span>element.<span style="color: #006600;">getValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&lt;br/&gt;&lt;br/&gt;Cache chargé dans l'objet mySecondMovie&lt;br/&gt;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Nom : &quot;</span> + mySecondMovie.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> +<span style="color: #ff0000;">&quot;&lt;br/&gt;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Description : &quot;</span> + mySecondMovie.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> +<span style="color: #ff0000;">&quot;&lt;br/&gt;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		out.<span style="color: #006600;">println</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Acteur : &quot;</span> + mySecondMovie.<span style="color: #006600;">getActor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> +<span style="color: #ff0000;">&quot;&lt;br/&gt;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

</p>
<p><strong>6) Le fichier web.xml mappant la servlet :</strong></p>
<p>
<i>Fichier : WEB-INF/web.xml</i></p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span><span style="font-weight: bold; color: black;">?&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;web-app</span> 	<span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/j2ee&quot;</span>
			<span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
			<span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;</span>
			<span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.4&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;display-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>ehCacheSample<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/display-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;description<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		Démonstration de l'utilisation du cache : EhCache par tellaw.org
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/description<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Declaration des servlets --&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>MovieServlet<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet-class<span style="font-weight: bold; color: black;">&gt;</span></span></span>com.tellaw.ehCacheSample.servlets.TestServlet<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet-class<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Declaration du mapping --&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>MovieServlet<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;url-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></span>/MovieServlet<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/url-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/web-app<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

</p>
<p><strong>Notes</strong>&nbsp;:</p>
<ul>
<li>La documentation officielle ne donne pas comme dépendance Jakarta Commons Collections. En ce qui me concerne, j&#8217;ai une exception type ClassNotFound sur <em>LRUMap</em>.</li>
<li>Notez que pour le passage en production de votre application, il faut remonter le niveau d&#8217;alerte de log4j, sinon le serveur va loguer les messages de debug.</li>
</ul>
<p><strong>Sites utiles</strong>&nbsp;:</p>
<ul>
<li>Le site de ehCache&nbsp;: <a href="http://ehcache.sourceforge.net/" hreflang="fr">ehcache</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.tellaw.org/2008/04/25/gerer-un-cache-avec-ehcache/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
