具有复杂键的Spring @Cacheable仍然执行 [英] Spring @Cacheable with complex keys still executed

查看:95
本文介绍了具有复杂键的Spring @Cacheable仍然执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在春天(3.1)使用@Cacheable时有以下内容:

I have the following for the usage of a @Cacheable in spring (3.1):

spring:

<?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>

要缓存的方法:

@Cacheable(value="cahceName", key="concat(#param1).concat(‘-’).concat(#param2)")
    public String cachedMethod(String param1,String param2)

唉,当我调试代码时,我看到调用了缓存的方法不止一次,即使param1和param2相同(即不使用cahce)。

Alas, when I debug the code, I see that the cached method gets called more than once even when param1 and param2 are the same (i.e the cahce is not used).

任何想法?

推荐答案

密钥显示不正确 -

The key does not appear correct -

您的意思可能是 - @Cacheable(值=cacheName,key =#param1.concat(' - ')。concat(#param2))

You may have meant - @Cacheable(value="cacheName", key="#param1.concat(‘-’).concat(#param2)")

此外,如果编译是在没有调试信息的情况下完成的,param1,param2参数名称将不可用于表达式求值程序。相反,您可以使用p0,p1等以这种方式引用它们:

Further, if the compilation is done without debug information, the param1, param2 argument names will not be available to expression evaluator. Instead you can refer to them using p0, p1 etc this way:

@Cacheable(value =cahceName,key =#p0。 concat(' - ')。concat(#p1))

更新:

我这里有一个单页测试,演示了这是如何工作的 - https:// gist.github.com/3315275

I have a one page test here which demonstrates how this works - https://gist.github.com/3315275

这篇关于具有复杂键的Spring @Cacheable仍然执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆