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