Spring @Cacheable仍在执行复杂键

问题描述 投票:6回答:2

我在Spring(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)

A,当我调试代码时,我看到即使param1和param2相同(即不使用cahce),也多次调用了该缓存方法。

有什么想法吗?

java spring caching
2个回答
15
投票

键显示不正确-

您可能是说-@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


0
投票

就我而言,问题是由于缓存提供程序(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}一样)。

© www.soinside.com 2019 - 2024. All rights reserved.