前面的章節,講解了Spring Boot集成Spring Cache,Spring Cache已經完成了多種Cache的實現,包括EhCache、RedisCache、ConcurrentMapCache等。java
這一節咱們來看看Spring Cache使用EhCache。git
EhCache是一個純Java的進程內緩存框架,具備快速、精幹等特色,Hibernate中的默認Cache就是使用的EhCache。github
本章節示例是在Spring Boot集成Spring Cache的源碼基礎上進行改造。源碼地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cacheweb
使用EhCache做爲緩存,咱們先引入相關依賴。spring
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--ehcache依賴--> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
而後建立EhCache的配置文件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" updateCheck="false"> <!-- 磁盤存儲:將緩存中暫時不使用的對象,轉移到硬盤,相似於Windows系統的虛擬內存 path:指定在硬盤上存儲對象的路徑 path能夠配置的目錄有: user.home(用戶的家目錄) user.dir(用戶當前的工做目錄) java.io.tmpdir(默認的臨時目錄) ehcache.disk.store.dir(ehcache的配置目錄) 絕對路徑(如:d:\\ehcache) 查看路徑方法:String tmpDir = System.getProperty("java.io.tmpdir"); --> <diskStore path="java.io.tmpdir" /> <!-- defaultCache:默認的緩存配置信息,若是不加特殊說明,則全部對象按照此配置項處理 maxElementsInMemory:設置了緩存的上限,最多存儲多少個記錄對象 eternal:表明對象是否永不過時 (指定true則下面兩項配置需爲0無限期) timeToIdleSeconds:最大的發呆時間 /秒 timeToLiveSeconds:最大的存活時間 /秒 overflowToDisk:是否容許對象被寫入到磁盤 說明:下列配置自緩存創建起600秒(10分鐘)有效 。 在有效的600秒(10分鐘)內,若是連續120秒(2分鐘)未訪問緩存,則緩存失效。 就算有訪問,也只會存活600秒。 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="true" /> <cache name="cache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="600" overflowToDisk="true" /> </ehcache>
而後SpringBoot配置文件中,指明緩存類型並聲明Ehcache配置文件的位置。springboot
server: port: 10900 spring: profiles: active: dev cache: type: ehcache ehcache: config: classpath:/ehcache.xml
這樣就能夠開始使用Ehcache了,測試代碼與Spring Boot集成Spring Cache一致。app
SpringBoot啓動類,@EnableCaching開啓Spring Cache緩存功能。框架
@EnableCaching @SpringBootApplication public class SpringbootApplication { public static void main(String[] args) { String tmpDir = System.getProperty("java.io.tmpdir"); System.out.println("臨時路徑:" + tmpDir); SpringApplication.run(SpringbootApplication.class, args); } }
CacheApi接口調用類,方便調用進行測試。spring-boot
@RestController @RequestMapping("cache") public class CacheApi { @Autowired private CacheService cacheService; @GetMapping("get") public User get(@RequestParam int id){ return cacheService.get(id); } @PostMapping("set") public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){ User u = new User(code, name); return cacheService.set(id, u); } @DeleteMapping("del") public void del(@RequestParam int id){ cacheService.del(id); } }
CacheService緩存業務處理類,添加緩存,更新緩存和刪除。
@Slf4j @Service public class CacheService { private Map<Integer, User> dataMap = new HashMap <Integer, User>(){ { for (int i = 1; i < 100 ; i++) { User u = new User("code" + i, "name" + i); put(i, u); } } }; // 獲取數據 @Cacheable(value = "cache", key = "'user:' + #id") public User get(int id){ log.info("經過id{}查詢獲取", id); return dataMap.get(id); } // 更新數據 @CachePut(value = "cache", key = "'user:' + #id") public User set(int id, User u){ log.info("更新id{}數據", id); dataMap.put(id, u); return u; } //刪除數據 @CacheEvict(value = "cache", key = "'user:' + #id") public void del(int id){ log.info("刪除id{}數據", id); dataMap.remove(id); } }
源碼地址:https://github.com/imyanger/springboot-project/tree/master/p22-springboot-cache-ehcache