SpringBoot緩存Ehcache的使用詳解
spring緩存(EhCache)是在Spring3.1開始引入的,但是其本身只提供了緩存接口,不提供具體緩存的實現(xiàn),其實現(xiàn)需要第三方緩存實現(xiàn)(Generic、EhCache、Redis等)。EhCache、Redis比較常用,使用Redis的時候需要先安裝Redis服務(wù)器。
為什么引入緩存
- 提升服務(wù)性能: 例如在項目開發(fā)完成以后,隨著時間推移,各種數(shù)據(jù)急劇增加,在數(shù)據(jù)不斷增加的情況下,一個簡單的Select * from Student,都可能非常耗時,變成我們用戶體驗的痛點。并且在分布式遠程調(diào)用的過程中,網(wǎng)絡(luò)開銷本來就比較大,如果再加上上面情況導致整體響應(yīng)時間變大,得不償失,因此緩存是十分必要的
- 減少數(shù)據(jù)庫壓力: 當數(shù)據(jù)增大,請求變多以后,數(shù)據(jù)庫的壓力將大大增加,緩存的出現(xiàn)可以減輕數(shù)據(jù)庫壓力。
SpringBoot抽象緩存
剛才說了Spring3.1引入了緩存接口,可以對接不同的緩存技術(shù)主要接口有:
- org.springframework.cache.Cache (定義緩存的接口)。
- org.springframework.cache.CacheManager:緩存管理器針對不同的緩存技術(shù),有不同的緩存管理器,SpringBoot會按照以下順序自動配置這些框架提供的緩存管理器。
- Generic。
- JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, and others)。
- EhCache 2.x。
- Hazelcast。
- Infinispan。
- Couchbase。
- Redis。
- Caffeine。
- Simple。
代碼實現(xiàn)
添加緩存依賴
在pom.xml中添加spring-boot-starter-cache。
<!--數(shù)據(jù)緩存-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>開啟緩存
使用注解**@EnableCaching**注解開啟緩存功能。
@Configuration
@EnableCaching
public class MyCacheConfig {
}數(shù)據(jù)緩存
在緩存操作中常用的注解有以下:
@Cacheable
@Cacheable可以標記在方法和類上面,在執(zhí)行的時候會先看緩存數(shù)據(jù)是否存在,如果存在直接返回緩存數(shù)據(jù),如果不存在就會支付方法并將緩存返回到緩存中,常用的三個屬性。
value:用于說明緩存的名稱,可以指定一個或者多個。
key:緩存的鍵值可以為空,如果不為空需要安裝SpEl表達方式編寫。
condition:緩存的條件,可以為空,如果使用按照SpEl方式編寫,返回true則緩存,false不緩存。
@Cacheable(value = "student",key = "#id",condition = "#id>11")
@Override
public Student findById(Long id) {
return studentMapper.findById(id);
}@CachePut
@CachePut可以標注在方法和類上面,常用屬性和**@Cacheable相同,不同之處在于執(zhí)行方法前不會查看緩存中是否存在,而是方法執(zhí)行完成以后將結(jié)果放入緩存中,多用于數(shù)據(jù)的添加和修改。
@CachePut(value = "student",key = "#student.id")
@Override
public Student updateStudent(Student student){
studentMapper.updateStudent(student);
return student;
}@CacheEvict
@CacheEvict可以標注在方法和類方面,用于清除緩存,常用注解除了和@Cacheable相同以外還有。
- allEntries: 是否清空所有緩存,默認false,當為true時,調(diào)用方法后就會清空所有緩存。
- beforeInvocation: 是否在方法執(zhí)行前情況,默認false,為true的時候,在方法調(diào)用前就會清空緩存,false的時候如果方法拋出異常則不會清除緩存。
@CacheEvict(value = "student",key = "#id",allEntries = true,beforeInvocation = true)
public void deleteStudent(@Param("id") Long id){
System.out.println("deleteStudent數(shù)據(jù)庫..." + id);
studentMapper.deleteStudent(id);
}集成EhCache
因為springboot只是緩存的抽象,要具體實現(xiàn)緩存還有依賴第三方緩存框架,我們這里介紹EhCache框架實現(xiàn)緩存。
添加EhCache依賴
在pom.xml中添加EhCache依賴。
<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.9.2</version>
</dependency>添加Ehcache相關(guān)配置
1、在src\main\resources路徑下添加ehcache.xml文件。
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<cache name="student"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="600"/>
</ehcache>注解含義:
- name: 緩存名稱和緩存注解中value屬性相同即可。
- maxElementsInMemory: 緩存的最大數(shù)量。
- overflowToDisk: 緩存達到最大數(shù)量,會寫入到磁盤。
- eternal: 緩存是否永久有效,如果設(shè)置為true,則timeout無效。
- diskExpiryThreadIntervalSeconds: 磁盤失效線程運行時間間隔,默認120s。
2、在application.yml添加ehcache.xml的路徑。
spring:
cache:
type: ehcache
ehcache:
config: classpath:/ehcache.xmlehcache.config的默認路徑為src\main\resourcesehcache.xm,所以也可以不配置。
測試
1、測試@Cacheable(value = "student",key = "#id",cndition = "#id>11")使用postman測試接口http://localhost:8899/student/select/11。
點擊兩次我們在console發(fā)現(xiàn),兩次都進入了方法,這是因為我們有判斷添加id大于11才會放入緩存中。

如果id>11例如http://localhost:8899/student/select/13,那么點擊兩次的情況下,我們只進入了方法一次。

其他測試可以自行測試,這里就不過多測試了。
到此這篇關(guān)于SpringBoot緩存Ehcache的使用的文章就介紹到這了,更多相關(guān)SpringBoot緩存Ehcache內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot controller接收txt文本文件實現(xiàn)方式
文章介紹在Spring Boot Controller中接收文本文件的方法,需使用MultipartFile類并通過@RequestParam注解獲取,重點說明了獲取文件名、內(nèi)容及保存文件的常用方法,并強調(diào)MultipartFile為接口,需通過注解實例化2025-09-09
淺析springcloud 整合 zipkin-server 內(nèi)存日志監(jiān)控
Zipkin是一款開源的分布式實時數(shù)據(jù)追蹤系統(tǒng)(Distributed Tracking System),其主要功能是聚集來自各個異構(gòu)系統(tǒng)的實時監(jiān)控數(shù)據(jù)。這篇文章主要介紹了springcloud 整合 zipkin-server 內(nèi)存日志監(jiān)控,需要的朋友可以參考下2019-11-11
flowable動態(tài)創(chuàng)建多級流程模板實現(xiàn)demo
這篇文章主要為大家介紹了flowable動態(tài)創(chuàng)建多級流程模板實現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
mybatis-plus實現(xiàn)四種lambda表達式方式
使用了lambda表達式 可以通過方法引用的方式來使用實體字段名的操作,本文主要介紹了mybatis-plus實現(xiàn)四種lambda表達式方式,具有一定的參考價值,感興趣的可以了解一下2024-06-06
Springboot詳解RocketMQ實現(xiàn)廣播消息流程
RocketMQ作為一款純java、分布式、隊列模型的開源消息中間件,支持事務(wù)消息、順序消息、批量消息、定時消息、消息回溯等,本篇我們了解如何實現(xiàn)廣播消息2022-06-06

