【问题标题】:Spring @Cacheable with complex keys still executed仍然执行复杂键的 Spring @Cacheable
【发布时间】:2012-08-07 00:48:11
【问题描述】:

我在春季(3.1)中使用@Cacheable有以下几点:

春天:

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

马文:

    <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)。

有什么想法吗?

【问题讨论】:

  • 注意:要使缓存起作用,您需要调用接口中的方法。
  • 您好清单 - 您能否指定两个建议中的哪一个解决了您的问题:key="#param1.concat('-').concat(#param2)" 或 key="#p0. concat('-').concat(#p1)" ??还是两者兼而有之?

标签: java spring caching


【解决方案1】:

密钥显示不正确 -

你的意思可能是 - @Cacheable(value="cacheName", key="#param1.concat(‘-’).concat(#param2)")

此外,如果在没有调试信息的情况下完成编译,则表达式评估器将无法使用 param1、param2 参数名称。相反,您可以通过这种方式使用 p0、p1 等来引用它们:

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

更新:

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

【讨论】:

  • 能否详细说明调试部分?为什么 p1 不同于 paam1
  • 这是一个相关的问题 - stackoverflow.com/questions/11041506/…
  • 我已经修复了密钥,但它仍然不起作用。我什至把钥匙完全拿走了,我的方法不断被调用。还有什么我可能做错了吗?
  • 我在这里有一个单页测试 - gist.github.com/3315275,它演示了您的场景,测试对我来说非常有效 - 该方法被调用一次然后缓存。
【解决方案2】:

在我的情况下,问题是由于使用了错误的缓存提供程序配置(Caffeine):

@Bean
public Caffeine<Object, Object> caffeineCacheBuilder() {
    return Caffeine.newBuilder()
        .initialCapacity(100)
        .maximumSize(1000)
        .expireAfterAccess(10, TimeUnit.MINUTES)
        .weakKeys(); // cause problems when concatenated keys used
}

正如文档所说,weakKeys() 方法:

指定存储在缓存中的每个键(不是值)都应包装在 WeakReference 中(默认情况下,使用强引用)。

警告: 使用此方法时,生成的缓存将使用标识 ({@code ==}) 比较以确定键的相等性。因此,它的 {@link Cache#asMap} 视图将 在技​​术上违反 {@link Map} 规范(与 {@link IdentityHashMap} 确实)。

【讨论】:

    猜你喜欢
    • 2012-05-07
    • 2014-11-26
    • 2017-01-15
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2019-11-19
    • 2020-11-25
    相关资源
    最近更新 更多