java - EhCache 2.6.0 的 ClassCastException

标签 java ehcache

我使用 EhCache 来存储应用程序对象的有限大小的缓存,但在升级其版本后,我在使用 cache.get(...) 时遇到此异常:

java.lang.ClassCastException: net.sf.ehcache.store.chm.SelectableConcurrentHashMap$DummyPinnedKey cannot be cast to br.com.project.util.VttcElement

有人知道我们的缓存 key 中是否应该实现任何新接口(interface)吗?

这是我改编的代码:

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;

private VttcElementCache() {
    CacheManager cacheManager = CacheManager.create(METAINFFileLoader.getInputStream("ehcache.xml"));
    cache = cacheManager.getCache("br.com.project.util.VttcElement");
}

private void load(int id, long idf) {
    cache.put(new Element(new VttcElement(id, idf), "1"));
}

private String get(VttcElement vttcElement) {
    Element element = cache.get(vttcElement); //The exception is thrown here!
    (String) return elemente.getValue();
}

最佳答案

我找到了。

由于某种原因,我的关键类 VttcElement 没有很好地存储在缓存中,因为异常是在其 equals 方法内部抛出的,就在它生成的行 (VttcElement) obj.

我将所有键更改为字符串,而不是使用其属性与逗号连接VttcElement,并且它完美地用作键。

非常感谢。

关于java - EhCache 2.6.0 的 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12605860/

相关文章:

java - Spring ehcache - 如果返回类型为 List<User>,#root.targetClass 值?

java - 负载平衡应用程序中的缓存失效

java - EHCache 2.10 中的缓存层次结构

java - 服务器启动时由于 ecache 请求负载过多而导致数据库服务器停止运行

javascript - 将变量从 jsp 页面传递到 servlet

java - 是否可以在 JavaFX 场景中显示 VolatileImage?

java - 是否可以在 JDBC 中执行 "field IN list"查询,并将列表作为参数传递?

java - <String> 有什么作用吗?

java - 查找元素所在的 JFrame