首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JCache密钥异常

JCache密钥异常
EN

Stack Overflow用户
提问于 2019-08-29 13:15:19
回答 1查看 1.6K关注 0票数 1

我有一个必须缓存的方法。我有一个带有JSR-107 (JCache)缓存的SpringBoot 2.1.7应用程序。使用的缓存实现: EHCache 3.8.0。

代码语言:javascript
复制
@Override
@HystrixCommand(fallbackMethod = "fallbackGetAllUserProfiles")
@Timed(histogram = true, percentiles = {0.75, 0.9, 0.95, 0.99})
@CacheResult
public UserProfiles getAllUserProfiles(@CacheKey String accountID, @CacheKey String userID) {
    return getAllUserProfilesFromBackend(accountID, userID);
}

当我在我的SpringBoot starter类上使用注释:@EnableCaching时,这个类是用CacheDefaults(cacheName = "profileCache")注释的,当我试图访问这个方法时,我得到了这个异常:

代码语言:javascript
复制
java.lang.ClassCastException: Invalid key type, expected : java.lang.String but was : org.springframework.cache.interceptor.SimpleKey
    at org.ehcache.impl.store.BaseStore.checkKey(BaseStore.java:64) ~[ehcache-3.8.0.jar:3.8.0 98c7461621c490ef009548e61849f925305a631f]
    at org.ehcache.impl.internal.store.heap.OnHeapStore.get(OnHeapStore.java:279) ~[ehcache-3.8.0.jar:3.8.0 98c7461621c490ef009548e61849f925305a631f]
    at org.ehcache.core.Ehcache.doGet(Ehcache.java:90) ~[ehcache-3.8.0.jar:3.8.0 98c7461621c490ef009548e61849f925305a631f]
    at org.ehcache.core.EhcacheBase.get(EhcacheBase.java:127) ~[ehcache-3.8.0.jar:3.8.0 98c7461621c490ef009548e61849f925305a631f]
    at org.ehcache.jsr107.Eh107Cache.get(Eh107Cache.java:90) ~[ehcache-3.8.0.jar:3.8.0 98c7461621c490ef009548e61849f925305a631f]
    at org.springframework.cache.jcache.JCacheCache.lookup(JCacheCache.java:77) ~[spring-context-support-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.cache.support.AbstractValueAdaptingCache.get(AbstractValueAdaptingCache.java:58) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.cache.interceptor.AbstractCacheInvoker.doGet(AbstractCacheInvoker.java:73) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.cache.jcache.interceptor.CacheResultInterceptor.invoke(CacheResultInterceptor.java:57) ~[spring-context-support-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.cache.jcache.interceptor.JCacheAspectSupport.execute(JCacheAspectSupport.java:135) ~[spring-context-support-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.cache.jcache.interceptor.JCacheAspectSupport.execute(JCacheAspectSupport.java:112) ~[spring-context-support-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.cache.jcache.interceptor.JCacheInterceptor.invoke(JCacheInterceptor.java:82) ~[spring-context-support-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]

缓存规格如下:

代码语言:javascript
复制
    <cache alias="profileCache">
        <key-type copier="org.ehcache.impl.copy.SerializingCopier">java.lang.String</key-type>
        <value-type copier="org.ehcache.impl.copy.IdentityCopier">java.util.List</value-type>
        <expiry>
            <ttl unit="minutes">5</ttl>
        </expiry>
        <heap unit="entries">10000</heap>
    </cache>

当我将注释更改为@EnableCaching(mode = AdviceMode.ASPECTJ)时,它确实起作用了。这是一个bug,还是我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2020-01-14 04:23:08

如果在没有任何参数的方法上进行缓存,但没有指定键,则会发生此错误。我通过添加一个键解决了这个错误:

代码语言:javascript
复制
@Cacheable(value = "SomeCacheName", key = "#root.methodName")
public List<Country> getCountries() {
    ...
    return countries;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57703392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档