Bonjour à tous
J'essaye d’utiliser un cache (ehcache en l'occurrence) dans la couche Service de mon projet
Voici ce que j'ai défini :
Sans le context xml de Spring :
dans le fichier ehcache.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:sec="http://www.springframework.org/schema/security" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <context:component-scan base-package="fr.masociete" /> <context:annotation-config /> <cache:annotation-driven />
et enfin les services sont annotés comme suit:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 <ehcache xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true" maxBytesLocalHeap="150M"> <diskStore path="java.io.tmpdir"/> <defaultCache eternal="false" maxElementsInMemory="100" overflowToDisk="false" /> <!-- Les accès aux tables de référence sont cachés durant 1h --> <cache name="cacheGetAllCodeActivite" eternal="false" timeToLiveSeconds="3600" /> <cache name="cacheGetCodeActiviteWithCode" eternal="false" timeToLiveSeconds="3600" /> <cache name="cacheGetAllOrganisation" eternal="false" timeToLiveSeconds="3600" /> <cache name="cacheGetOrganisationWithCode" eternal="false" timeToLiveSeconds="3600" /> </ehcache>
Quand je trace l'application, l'appel à la méthode sans paramètre "cacheGetAllOrganisation" est bien stockée en cache donc l'appel au service et au dao en dessous n'est fait qu'une seule fois.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 @Cacheable(value = "cacheGetAllOrganisation") public List<Organisation> getAllOrganisation() throws PeriodeException, MoreThanOneResultException { return organisationDao.getAllOrganisation(); } @Cacheable(value = "cacheGetOrganisationWithCode", key = "#code") public Organisation getOrganisationWithCode(final String code) throws PeriodeException, MoreThanOneResultException { return organisationDao.getOrganisationWithCode(code); }
1er appel : getAllOrganisation() -> service -> dao -> SGBD
2eme appel : getAllOrganisation() -> cache
3eme appel : getAllOrganisation() -> cache
par contre la seconde méthode avec paramètre (cacheGetOrganisationWithCode) n'est jamais "cachée". l'appel au dao et à la base est fait systématiquement quelque soit la valeur du paramètre
1er appel : getOrganisationWithCode('PUS') -> service -> dao -> SGBD
2eme appel : getOrganisationWithCode('PUS') -> service -> dao -> SGBD
3eme appel : getOrganisationWithCode('PUS') -> service -> dao -> SGBD
Avez vous une idée sur ce "non" fonctionnement du cache si paramètre ?
Partager