Ajouter une dépendance dans le domain-service de l'application (fichier : appli-domain-services/pom.xml). Pour plus d'informations : http://code.google.com/p/ehcache-spring-annotations/
<dependency> <groupId>com.googlecode.ehcache-spring-annotations</groupId> <artifactId>ehcache-spring-annotations</artifactId> <version>1.2.0</version> </dependency> |
Déclarer le cacheManager de ehCache dans les properties du module "vue", cache.xml
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"> <!-- /properties/cache/cache.xml The configuration file for caching features. This file should not be modified but by the developers. This file is referenced by /properties/applicationContext.xml. --> <ehcache:annotation-driven/> <aop:config proxy-target-class="true" /> <bean id="cacheManager" > <description> This bean is used to configure EhCache. </description> <property name="configLocation" value="classpath:/properties/cache/ehcache.xml" /> </bean> </beans> |
La configuration du cache se paramètre via le fichier de configuration ehcache.xml. Ces paramètres sont accessibles depuis la documentation du framework ehCache : http://ehcache.org/documentation/configuration.html
<ehcache> <!-- /properties/cache/ehcache.xml The configuration file for EhCache. This file is referenced from /properties/cache/cache.xml. --> <diskStore path="/tmp/cache"/> <cache name="ehCalculatorController" maxElementsInMemory="10000" maxElementsOnDisk="10000" eternal="false" timeToIdleSeconds="1800" timeToLiveSeconds="1800" overflowToDisk="true" /> </ehcache> |
Attributs du cache
name : identifiant du cache
maxElementsInMemory : nombre d'éléments maximum gardé en cache. (Lorsque ce nombre est atteint l'élément LRU est supprimé du cache)
maxElementsOnDisk : nombre d'éléments maximum gardé sur le disque.
eternal : Définit si les éléments expirent au bout d'un certain temps ou pas.
timeToIdleSeconds : Détermine le temps d'attente avant qu'un élément expire après une période de temps durant laquelle il n'a pas été utilisé.
timeToLiveSeconds : Détermine le temps d'attente entre la création d'un élément et sa suppression du cache.
overflowToDisk : Permet d'écrire sur le disque si la mémoire allouée est dépassée mais le nombre d'éléments maximum n'est pas atteint.
Pour chaque méthode de type get on ajoute l'annotation @Cacheable avec pour attribut name un identifiant qui servira à identifier le cache associé à la méthode.
public class CalculatorController { @Cacheable(cacheName = ehCalculatorController) public CalculElp getNewCalculElp(ElpPojo elpPojo) { ... } } |