我正在尝试设置一个 EHCache,它将用于在同一 Tomcat 实例上运行的各种 WAR 文件。
我正在缓存的对象是自定义 java bean。我正在使用 EHCache 版本 2.8.3
理想情况下,我希望只在 Tomcat 通用类加载器中共享 ehcache JAR 来执行此操作,据我所知,这应该是可能的。
我正在做的测试有两个 Web 服务,ServiceA 和 ServiceB,它们使用与 Tomcat 公共(public)库文件夹中的 EHCache JAR 相同的缓存。
当我向 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-on-read
缓存配置:
<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) {
最佳答案
关于java - 使用来自共享类加载器的 EHCache 时出现 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25727002/