1、 Application.java中增加註解 //增加ehcache緩存 @EnableCaching public class Application {
2、配置文件ehcache.xml 存放到
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> <!-- 磁盤緩存位置 --> <diskStore path="java.io.tmpdir/Tmp_EhCache"/> <!-- 默認緩存 --> <defaultCache eternal="false" maxElementsInMemory="10000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="1800" timeToLiveSeconds="259200" memoryStoreEvictionPolicy="LRU"/> <!-- helloworld緩存 --> <cache name="cacheTestBean" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="50000" timeToLiveSeconds="50000" overflowToDisk="true" memoryStoreEvictionPolicy="LRU"/> <!--ehcache.xml配置參數說明: name:緩存名稱。 maxElementsInMemory:緩存最大個數。 eternal:緩存中對象是否爲永久的,如果是,超時設置將被忽略,對象從不過期。 timeToIdleSeconds:置對象在失效前的允許閒置時間(單位:秒)。僅當eternal=false對象不是永久有效時使用,可選屬性,默認值是0,也就是可閒置時間無窮大。 timeToLiveSeconds:緩存數據的生存時間(TTL),也就是一個元素從構建到消亡的最大時間間隔值,這隻能在元素不是永久駐留時有效,如果該值是0就意味着元素可以停頓無窮長的時間。 maxEntriesLocalDisk:當內存中對象數量達到maxElementsInMemory時,Ehcache將會對象寫到磁盤中。 overflowToDisk:內存不足時,是否啓用磁盤緩存。 diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一個緩衝區。 maxElementsOnDisk:硬盤最大緩存個數。 diskPersistent:是否在VM重啓時存儲硬盤的緩存數據。默認值是false。 diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。 memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。默認策略是LRU(最近最少使用)。你可以設置爲FIFO(先進先出)或是LFU(較少使用)。 clearOnFlush:內存數量最大時是否清除。--> </ehcache>
3、service調用
package com.unidt.norter.serviceImpl.normal; import com.qcloud.cos.utils.StringUtils; import com.unidt.norter.bean.UserInfo; import com.unidt.norter.bean.testcache.CacheTestBean; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @Description: 緩存測試服務 * @author songchun.yang * @Description: * @Date Created in 2020/4/1 16:36 */ @Service @CacheConfig(cacheNames="cacheTestBean") public class CacheTestService { /**模擬數據庫進行存儲**/ public Map<String, CacheTestBean> list = new HashMap<String,CacheTestBean>(); @Cacheable(key="#bean.getKey()") public CacheTestBean save(CacheTestBean bean) { System.out.println("save()被執行了...."); return bean; } /** *@Description: 添加到map *@Author: ysc 2020/4/1 16:36 */ @CachePut(value = "cacheTestBean", key="#cacheTestBean.key",condition = "#cacheTestBean.name != null ") public CacheTestBean add(CacheTestBean cacheTestBean){ if(null!=cacheTestBean &&!StringUtils.isNullOrEmpty(cacheTestBean.getKey()) &&!list.containsKey(cacheTestBean.getKey())){ /**設置創建時間**/ cacheTestBean.setCreatTime(new Date()); System.out.println("添加到內存:"+cacheTestBean); return list.put(cacheTestBean.getKey(),cacheTestBean); } return new CacheTestBean(); } /** *@Description: 獲取map *@Author: ysc 2020/4/1 16:36 */ @Cacheable(value = "cacheTestBean",key="#key", condition = "#key != null ") public CacheTestBean get(String key){ String a = "wqw"; CacheTestBean current=list.get(key); /**設置更新時間**/ if(current!=null){ current.setUpdateTime(new Date()); list.put(key,current); System.out.println("讀取內存:"+current); }else{ System.out.println("內存中不存在:"+current); } return current; /* if(!StringUtils.isNullOrEmpty(key)&&list.containsKey(key)){ CacheTestBean current=list.get(key); *//**設置更新時間**//* current.setUpdateTime(new Date()); list.put(key,current); System.out.println("讀取內存:"+current); return current; }*/ // return new CacheTestBean(); } /** *@Description: 刪除map *@Author: ysc 2020/4/1 16:36 */ @CacheEvict(value = "cacheTestBean",key="#key",condition = "#key != null ") public CacheTestBean del(String key){ if(!StringUtils.isNullOrEmpty(key)&&list.containsKey(key)){ System.out.println("刪除內存:"+key); return list.remove(key); } return new CacheTestBean(); } }
4、調用測試類
package com.unidt.norter.bean.testcache; import java.io.Serializable; import java.util.Date; /** * * @Description: 緩存測試實體 * @author songchun.yang * @Description: * @Date Created in 2020/4/1 16:37 */ public class CacheTestBean implements Serializable{ /**鍵**/ String key; /**值**/ String name; /**創建時間**/ Date creatTime; /**更新時間**/ Date updateTime; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreatTime() { return creatTime; } public void setCreatTime(Date creatTime) { this.creatTime = creatTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } @Override public String toString() { return "CacheTestBean{" + "key='" + key + '\'' + ", name='" + name + '\'' + ", creatTime=" + creatTime + ", updateTime=" + updateTime + '}'; } }
5、緩存路由
package com.unidt.api; import com.unidt.norter.bean.testcache.CacheTestBean; import com.unidt.norter.serviceImpl.normal.CacheTestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author songchun.yang * @Description: * @Date Created in 2020/4/1 17:09 */ @RestController @RequestMapping("/Cache") public class CacheTestController { @Autowired CacheTestService cacheTestService; /** *@Description: 測試緩存 *@param cacheTestBean *@return com.zyj.bean.CacheTestBean *@Author: zyj 2018/5/26 22:47 */ @RequestMapping("/add") public CacheTestBean testAdd(CacheTestBean cacheTestBean){ return cacheTestService.add(cacheTestBean); } /** *@Description: 測試獲取緩存 *@param key *@return com.zyj.bean.CacheTestBean *@Author: zyj 2018/5/26 22:47 */ @RequestMapping("/get") public CacheTestBean testGet(String key){ CacheTestBean current=cacheTestService.get(key); System.out.println("獲得數據"+current); return current; } /** *@Description: 測試刪除緩存 *@param key *@return com.zyj.bean.CacheTestBean *@Author: zyj 2018/5/26 22:47 */ @RequestMapping("/del") public CacheTestBean testDel(String key){ return cacheTestService.del(key); } }