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