【问题标题】:ClassCastException with EhCache 2.6.0EhCache 2.6.0 的 ClassCastException
【发布时间】:2012-09-27 04:05:06
【问题描述】:

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

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

有人知道我们是否应该在缓存键中实现任何新接口吗?

这是我改编的代码:

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();
}

【问题讨论】:

  • 能否提供一些代码?
  • 当然,我用它编辑了我的问题。谢谢。
  • 抱歉,我必须用真正的异常抛出点进行编辑。它在 cache.get 上。
  • 那里没有你应该实现的东西,但是有一个版本的错误。我需要仔细检查...但是您看到的异常与固定有关。您似乎没有这样做...您是否使用具有相同缓存的 Hibernate ?

标签: java ehcache


【解决方案1】:

我找到了。

由于某种原因,我的关键类 VttcElement 没有很好地存储在缓存中,因为异常是在它的 equals 方法中引发的,就在它生成 的行中(VttcElement) 对象.

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

非常感谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多