【发布时间】:2016-09-20 03:16:22
【问题描述】:
我在企业应用程序中将 EhCache v2.3.0 与 EclipseLink v2.5.2 和 EJB 3.0 结合使用。
问题是,EhCache 的对象没有更新。预期的行为是,缓存在 60 秒后过期并重新加载数据。实际发生的是,缓存在 60 秒后过期并重新加载旧数据。
即使实体缓存设置为“隔离”,也会发生这种情况。出于测试目的,我什至将缓存类型设置为无,但它不起作用。
有人有想法吗?
这里是ehcache.xml:
<ehcache name="testCache">
<defaultCache
timeToIdleSeconds="60"
timeToLiveSeconds="60"/>
<cache name="myCache"
timeToIdleSeconds="60"
timeToLiveSeconds="60"/></ehcache>
这是在 Context-Listener 启动时正确加载的。所以缓存设置了正确的值,我在调试模式下检查过。
缓存初始化后(它是一个单例),它是用这个方法访问的:
/*
This method returns the current values in the cache as an list.
If the elements expired, it gets the new values from the database, puts them into the cache and return them as list.
*/
public List<CacheEntries> getEntries() {
EhCache cache = cacheManager.getEhCache("myCache");
cache.evictExpiredElements();
List<CacheEntries> list = new ArrayList<CacheEntries>();
if(cache.getSize() == 0) {
//get CacheEJB
list = cacheEjb.getAll();
for(CacheEntries e : list) {
cache.put(new Element(e.getName(), e));
}
}
else {
Element element = null;
List<String> keys = cache.getKeys();
for(String s : keys) {
element = cache.get(s);
list.add((CacheEntries) element.getValue());
}
}
return list;
}
因此实体被注释:
@Entity @Cache(type = CacheType.NONE, isolation = CacheIsolationType.ISOLATED) @Table ...
【问题讨论】:
-
另外添加缓存事件侦听器是一个很好的做法。你可以真正确定发生了什么。 ehcache.org/documentation/2.8/apis/cache-event-listeners.html
标签: jpa jakarta-ee caching eclipselink ehcache