【发布时间】:2015-01-06 00:09:58
【问题描述】:
有没有办法在 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 的等价物。
识别特定对象的数据是否已过期或该对象根本不存在于缓存中的推荐方法是什么?
【问题讨论】:
-
你为什么不简单地使用 Spring Cache Abstraction?这将为您节省大量样板代码...
-
感谢Deinum 的回复。我怎么做?能给个链接吗?
-
Spring 参考指南中有一个关于caching 的部分。
标签: java spring caching ehcache