Exemple de l'intégration de la gestion du cache via les annotations ehcache pour Spring dans esup-commons-v2
Gestion des dépendances
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/
Bloc de code |
---|
<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
Bloc de code |
---|
<?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
Bloc de code |
---|
<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="org.esupportail.ects.domain.beans.User" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="30" timeToLiveSeconds="60" overflowToDisk="true" /> <cache name="ehCalculatorController" maxElementsInMemory="10000" maxElementsOnDisk="10000" eternal="false" timeToIdleSeconds="1800" timeToLiveSeconds="1800" overflowToDisk="true" /> </ehcache> |
Attributs du cache
...
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.
Bloc de code |
---|
public class CalculatorController { @Cacheable(cacheName = ehCalculatorController) public CalculElp getNewCalculElp(ElpPojo elpPojo) { ... } } |
...
|