spring boot + ehcache配置

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());
   }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章