从共享类加载器使用EHCache时出现ClassCastException



我正在尝试设置一个EHCache,它将在同一Tomcat实例上运行的各种WAR文件中使用。

我缓存的对象是自定义的javabean。我使用的是EHCache 2.8.3版

理想情况下,我希望只在Tomcat公共类加载器中共享ehcacheJAR,根据我所读到的内容,这应该是可能的。

我正在做的测试有两个web服务,ServiceA和ServiceB,它们使用的缓存与Tomcat公共lib文件夹中的EHCacheJAR相同。

当我使用ServiceA发出请求时,结果对象会成功插入到缓存中。

当我向ServiceB发出相同的请求时,结果对象会成功地从缓存中检索到。

然而,当我返回ServiceA并发出请求时,它会抛出一个ClassCastException

<faultcode>S:Server</faultcode>
 <faultstring>com.company.platform.auth.user.AuthenticatedUser cannot be cast to com.company.platform.auth.user.AuthenticatedUser</faultstring>

EHCache文档建议我应该将copyOnRead属性设置为true,以确保缓存始终使用序列化。然而,在中添加这个似乎对我不起作用

http://terracotta.org/documentation/3.7.4/enterprise-ehcache/configuration-guide#copy-读取

缓存配置:

<cache name="myCache"
       maxElementsInMemory="2000"
       copyOnRead="true"
       copyOnWrite="true"
       eternal="false"
       overflowToDisk="false"
       timeToIdleSeconds="0"
       timeToLiveSeconds="3600"
       memoryStoreEvictionPolicy="LFU"
       transactionalMode="off"
/>

缓存数据使用方法上的Spring@Cacheable注释进行管理:

@Cacheable(
        value=CACHE_NAME, 
        key="#username + ':' + T(com.company.platform.auth.handlers.EncryptionHandler).encrypt(#password)",
        unless="#result == null")
public AuthenticatedUser authenticate(String username, String password) {

这是Ehcache中的一个错误。

参考:https://jira.terracotta.org/jira/browse/EHC-976

它在2.6.2版本中被修复

最新更新