首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ehcache中配置对象级缓存

在ehcache中配置对象级缓存
EN

Stack Overflow用户
提问于 2015-01-05 23:02:26
回答 1查看 384关注 0票数 0

有没有办法在ehcache中配置对象或类级缓存(每个缓存都有不同的设置)?我正在使用java+spring+mybatis堆栈。

另外,ehcache等同于下面的基于oscache的实现吗?

代码语言:javascript
复制
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的东西。

识别特定对象的数据是否已过期或该对象是否根本不存在于缓存中的推荐方法是什么?

EN

回答 1

Stack Overflow用户

发布于 2015-01-06 00:04:26

您需要在EHcache对象上调用getCacheConfiguration。然后,您可以修改其fields

下面是一些检查过期或丢失的示例代码。注意getQuiet的使用,以确保您不会通过查看它来重置过期时间。

代码语言:javascript
复制
  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;
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27782171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档