spring boot + ehcache配置

1 篇文章 0 订阅
1 篇文章 0 订阅

1.填加dependency

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
   <groupId>net.sf.ehcache</groupId>
   <artifactId>ehcache</artifactId>
</dependency>

2.编写ehcache.xml放在resources下面,spring boot会自动加载

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>

    <diskStore path="java.io.tmpdir"/>

    <!-- DefaultCache setting. -->
    <defaultCache maxElementsInMemory="10000" memoryStoreEvictionPolicy="LRU" eternal="false"
                  diskPersistent="false" overflowToDisk="true"/>

    <!-- ComponentUpgrade 缓存1分钟-->
    <cache name="ComponentUpgrade" overflowToDisk="false" eternal="false" diskPersistent="false"
           maxElementsInMemory="2" memoryStoreEvictionPolicy="LRU" timeToLiveSeconds="60">
    </cache>

    <!-- RequireList 缓存10分钟-->
    <cache name="RequireList" overflowToDisk="false" eternal="false" diskPersistent="false"
           maxElementsInMemory="2" memoryStoreEvictionPolicy="LRU" timeToLiveSeconds="600">
    </cache>
</ehcache>
3.spring boot 主程序加@EnableCaching
@EnableCaching
@SpringBootApplication
@ServletComponentScan
@EnableCaching
public class OmtApplication {

    public static void main(String[] args) {
      SpringApplication application = new SpringApplication(OmtApplication.class);
      ApplicationUtil.setContext(application.run(args));
   }


 

4.在java代码中使用注解

@Cacheable(value = "RequireList", key = "'RequireServiceList'", unless = "#result == null")
public Map<String, List<RequireResultView>> list() {}

5.缓存观察类,辅助查看缓存用的

@Controller
@RequestMapping("/common/cache")
public class CacheController extends BaseController {
   /**
    *  Clears the contents of all caches in the CacheManager, but without removing any caches.
    *  It only guarantees to clear those elements in a cache at the time that the Ehcache.removeAll() mehod on each cache is called
    * @return
    */
   @RequestMapping("/clearAll.action")
   @ResponseBody
   public AjaxResult clearAll() {
      CacheManager.getInstance().clearAll();
      return successResult();
   }

   /**
    *
    * @param cacheName 即我们缓存中的value
    * @return
    */
   @RequestMapping("/removeAll.action")
   @ResponseBody
   public AjaxResult removeAll(String cacheName) {
      CacheManager manager = CacheManager.getInstance();
      Cache cache = manager.getCache(cacheName);
      if (cache == null) {
         return failureResult("不存在的cache:" + cacheName);
      }
      cache.removeAll();
      return successResult();
   }

   /**
    *
    * @param cacheName 即我们缓存中的value
    * @return
    */
   @RequestMapping("/listValues.action")
   @ResponseBody
   public AjaxResult listValues(String cacheName) {
      CacheManager manager = CacheManager.getInstance();
      Cache cache = manager.getCache(cacheName);
      if (cache == null) {
         return failureResult("不存在的cache:" + cacheName);
      }
      List keys = cache.getKeys();
      Map<Object, Element> values = cache.getAll(keys);
      logger.info("********************** list values start **********************");
      StringBuilder valuesStr = new StringBuilder();
      for (Map.Entry<Object, Element> entry : values.entrySet()) {
         valuesStr.append(entry.getKey());
         valuesStr.append(":");
         valuesStr.append(JSONUtils.toJson(entry.getValue().getObjectValue()));
      }
      logger.info("********************** list values end **********************");
      return successResult(valuesStr.toString());
   }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值