有没有办法在ehcache中配置对象或类级缓存(每个缓存都有不同的设置)?我正在使用java+spring+mybatis堆栈。
另外,ehcache等同于下面的基于oscache的实现吗?
public Map<Integer, ProductDetails> getProductDetails(final List<Integer> productIds)
throws Exception{
Map<Integer, ProductDetails> result = null;
try{
//Get from cache.
result = (Map<Integer, ProductDetails>) cache.getFromCache("AllProductDetails");
if(result == null){
throw new NeedsRefreshException("Cache needs a refresh!");
}
}
catch(final NeedsRefreshException nre){
try{
result = ProductDetailsDao.getProductDetails("");
cache.putInCache("AllProductDetails", result);
}
catch (final Exception e){
result = (Map<Integer, ProductDetails>) nre.getCacheContent();
cache.cancelUpdate("AllProductDetails");
}
}
return result;
}我发现在ehcache中没有等同于com.opensymphony.oscache.base.NeedsRefreshException的东西。
识别特定对象的数据是否已过期或该对象是否根本不存在于缓存中的推荐方法是什么?
发布于 2015-01-06 00:04:26
您需要在EHcache对象上调用getCacheConfiguration。然后,您可以修改其fields。
下面是一些检查过期或丢失的示例代码。注意getQuiet的使用,以确保您不会通过查看它来重置过期时间。
public boolean expired(final K key) {
boolean expired = true;
// Do a quiet get so we don't change the last access time.
final Element element = cache.getQuiet(key);
if (element != null) {
expired = cache.isExpired(element);
if (expired) {
log.trace("Expired because expired.");
} else {
expired = element.getObjectValue() == null;
if (expired) {
log.trace("Expired because value null.");
}
}
} else {
log.trace("Expired because not present.");
}
return expired;
}https://stackoverflow.com/questions/27782171
复制相似问题