Printemps @Cacheable complexe, avec des touches encore à exécuter
J'ai le texte suivant pour l'utilisation d'un @Cacheable au printemps (3.1):
printemps:
<?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:oauth="http://www.springframework.org/schema/security/oauth2"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/security/oauth2
http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
<!-- Ehcache library setup -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml" />
Maven:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.5.3</version>
</dependency>
La mise en cache de la méthode:
@Cacheable(value="cahceName", key="concat(#param1).concat(‘-’).concat(#param2)")
public String cachedMethod(String param1,String param2)
Hélas, quand je déboguer le code, je vois que la mise en cache méthode est appelée plus d'une fois même quand param1 et param2 sont les mêmes (j'.e le cahce n'est pas utilisé).
Des idées?
- remarque: pour le cache de travail, vous avez besoin d'appeler une méthode qui est dans une interface.
- Salut Liste de contrôle - Pouvez-vous veuillez préciser lequel des deux recommandations résolu votre problème : key="#param1.concat(‘-’).concat(#param2)" ou key="#p0.concat('-').concat(#p1)" ?? ou les deux?
- SALUT. Les deux travaillé
Vous devez vous connecter pour publier un commentaire.
La clé ne semble pas correcte -
Vous avez peut-être voulu dire -
@Cacheable(value="cacheName", key="#param1.concat(‘-’).concat(#param2)")
De plus, si la compilation se fait sans les informations de débogage, le param1, param2 argument noms ne seront pas disponibles à l'évaluateur d'expression. Au lieu de cela vous pouvez vous référer à l'aide de p0, p1, etc de cette façon:
@Cacheable(value="cahceName", key="#p0.concat('-').concat(#p1)")
Mise à jour:
J'ai une page de test qui montre comment cela fonctionne - https://gist.github.com/3315275