一、EhCache使用演示
EhCache是一個純Java的進程內緩存框架,具有快速、精干等特點,Hibernate中的默認Cache就是使用的EhCache。
本章節示例是在Spring Boot集成Spring Cache的源碼基礎上進行改造。源碼地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache
使用EhCache作為緩存,我們先引入相關依賴。
<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配置文件的位置。
server: port: 10900 spring: profiles: active: dev cache: type: ehcache ehcache: config: classpath:/ehcache.xml
這樣就可以開始使用Ehcache了,測試代碼與Spring Boot集成Spring Cache一致。
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接口調用類,方便調用進行測試。
@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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。