在Spring Boot中如何使用數(shù)據(jù)緩存
在實(shí)際開發(fā)中,對于要反復(fù)讀寫的數(shù)據(jù),最好的處理方式是將之在內(nèi)存中緩存一份,頻繁的數(shù)據(jù)庫訪問會造成程序效率低下,同時(shí)內(nèi)存的讀寫速度本身就要強(qiáng)于硬盤。Spring在這一方面給我們提供了諸多的處理手段,而Spring Boot又將這些處理方式進(jìn)一步簡化,接下來我們就來看看如何在Spring Boot中解決數(shù)據(jù)緩存問題。
創(chuàng)建Project并添加數(shù)據(jù)庫驅(qū)動(dòng)
Spring Boot的創(chuàng)建方式還是和我們前文提到的創(chuàng)建方式一樣,不同的是這里選擇添加的依賴不同,這里我們添加Web、Cache和JPA依賴,如下圖:
創(chuàng)建成功之后,接下來添加數(shù)據(jù)庫驅(qū)動(dòng),我還是使用MySQL,在pom.xml中添加數(shù)據(jù)庫驅(qū)動(dòng),如下:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency>
配置application.properties
這個(gè)application.properties的配置還是和初識在Spring Boot中使用JPA一樣,各個(gè)參數(shù)的含義我這里也不再贅述,我們直接來看代碼:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/sang?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=sang spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jackson.serialization.indent_output=true
創(chuàng)建實(shí)體類
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
private String address;
private Integer age;
public Person() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Person(Long id, String name, String address, Integer age) {
this.id = id;
this.name = name;
this.address = address;
this.age = age;
}
}
創(chuàng)建實(shí)體類的Repository
public interface PersonRepository extends JpaRepository<Person,Long> {}
創(chuàng)建業(yè)務(wù)類
業(yè)務(wù)接口
public interface DemoService {
public Person save(Person person);
public void remove(Long id);
public Person findOne(Person person);
}
實(shí)現(xiàn)類
@Service
public class DemoServiceImpl implements DemoService {
@Autowired
PersonRepository personRepository;
@CachePut(value = "people", key = "#person.id")
@Override
public Person save(Person person) {
Person p = personRepository.save(person);
System.out.println("為id、key為" + p.getId() + "數(shù)據(jù)做了緩存");
return p;
}
@CacheEvict(value = "people")
@Override
public void remove(Long id) {
System.out.println("刪除了id、key為" + id + "的數(shù)據(jù)緩存");
personRepository.delete(id);
}
@Cacheable(value = "people", key = "#person.id")
@Override
public Person findOne(Person person) {
Person p = personRepository.findOne(person.getId());
System.out.println("為id、key為" + p.getId() + "數(shù)據(jù)做了緩存");
return p;
}
}@Service
public class DemoServiceImpl implements DemoService {
@Autowired
PersonRepository personRepository;
@CachePut(value = "people", key = "#person.id")
@Override
public Person save(Person person) {
Person p = personRepository.save(person);
System.out.println("為id、key為" + p.getId() + "數(shù)據(jù)做了緩存");
return p;
}
@CacheEvict(value = "people")
@Override
public void remove(Long id) {
System.out.println("刪除了id、key為" + id + "的數(shù)據(jù)緩存");
personRepository.delete(id);
}
@Cacheable(value = "people", key = "#person.id")
@Override
public Person findOne(Person person) {
Person p = personRepository.findOne(person.getId());
System.out.println("為id、key為" + p.getId() + "數(shù)據(jù)做了緩存");
return p;
}
}
關(guān)于這個(gè)實(shí)現(xiàn)類我說如下幾點(diǎn):
1.@CachePut表示緩存新添加的數(shù)據(jù)或者更新的數(shù)據(jù)到緩存中,兩個(gè)參數(shù)value表示緩存的名稱為people,key表示緩存的key為person的id
2.@CacheEvict表示從緩存people中刪除key為id的數(shù)據(jù)
3.@Cacheable表示添加數(shù)據(jù)到緩存中,緩存名稱為people,緩存key為person的id屬性。
創(chuàng)建Controller
@RestController
public class CacheController {
@Autowired
DemoService demoService;
@RequestMapping("/put")
public Person put(Person person) {
return demoService.save(person);
}
@RequestMapping("/able")
public Person cacheable(Person person) {
return demoService.findOne(person);
}
@RequestMapping("/evit")
public String evit(Long id) {
demoService.remove(id);
return "ok";
}
}
OK ,做完這一切我們就可以來測試我們剛剛寫的緩存了。
測試
看我們的Controller,我們有三個(gè)地址要測試,一個(gè)一個(gè)來。當(dāng)然,在 測試之前,我們先來看看初始狀態(tài)下的數(shù)據(jù)庫是什么樣子的:

首先我們在瀏覽器中訪問http://localhost:8080/able?id=1,得到如下訪問結(jié)果:
這個(gè)時(shí)候查看控制臺,輸出內(nèi)容如下:
說是數(shù)據(jù)已經(jīng)被緩存了,這個(gè)時(shí)候我們再繼續(xù)在瀏覽器中刷新繼續(xù)請求id為1的數(shù)據(jù),會發(fā)現(xiàn)控制臺不會繼續(xù)打印日志出來,就是因?yàn)閿?shù)據(jù)已被存于緩存之中了。
接下來我們向?yàn)g覽器中輸入http://localhost:8080/put?age=47&name=奧巴牛&address=米國,訪問結(jié)果如下:
這個(gè)時(shí)候查看控制臺打印的日志如下:
再查看數(shù)據(jù)表,數(shù)據(jù)已插入成功:
此時(shí),我們在瀏覽器中輸入http://localhost:8080/able?id=106,訪問剛剛插入的這條數(shù)據(jù),結(jié)果如下:
這個(gè)時(shí)候查看控制臺,發(fā)現(xiàn)并沒有數(shù)據(jù)數(shù)據(jù),就是因?yàn)閿?shù)據(jù)已經(jīng)處于緩存中了。
最后我們在瀏覽器中輸入http://localhost:8080/evit?id=106,將數(shù)據(jù)從緩存中移除,訪問結(jié)果如下:
這個(gè)時(shí)候查看控制臺,已經(jīng)提示緩存移除掉了:
同時(shí)數(shù)據(jù)也從數(shù)據(jù)庫刪除掉了,這個(gè)時(shí)候如果還需要該數(shù)據(jù)則需要我們繼續(xù)向表中添加數(shù)據(jù)。
緩存技術(shù)切換
Spring Boot默認(rèn)情況下使用ConcurrentMapCacheManager作為緩存技術(shù),有的時(shí)候你可能想替換為其他的緩存方式,在Spring Boot中進(jìn)行緩存的切換非常簡單,我這里以Google提供的Guava為例,如果要使用這種緩存策略,只需要添加相應(yīng)的依賴即可,如下:
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>20.0</version> </dependency>
就這樣就可以了。實(shí)際上在Spring Boot中,底層使用哪一種緩存我們并不必做過多考慮,切換的方式也很簡單,如上文引入相應(yīng)的依賴即可,我們只需要把上層的邏輯寫好即可。
本文案例下載:
本文GitHub地址https://github.com/lenve/JavaEETest/tree/master/Test25-Cache.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring Boot Web 靜態(tài)文件緩存處理的方法
- spring boot+spring cache實(shí)現(xiàn)兩級緩存(redis+caffeine)
- Spring Boot 中使用cache緩存的方法
- SpringBoot項(xiàng)目中使用redis緩存的方法步驟
- 實(shí)例詳解Spring Boot實(shí)戰(zhàn)之Redis緩存登錄驗(yàn)證碼
- Spring Boot緩存實(shí)戰(zhàn) EhCache示例
- Spring Boot 基于注解的 Redis 緩存使用詳解
- 詳解Spring Boot使用redis實(shí)現(xiàn)數(shù)據(jù)緩存
- Spring Boot集成Redis實(shí)現(xiàn)緩存機(jī)制(從零開始學(xué)Spring Boot)
- 在Spring Boot中實(shí)現(xiàn)HTTP緩存的方法
相關(guān)文章
Java?RabbitMQ的持久化和發(fā)布確認(rèn)詳解
這篇文章主要為大家詳細(xì)介紹了RabbitMQ的持久化和發(fā)布確認(rèn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
解析springBoot-actuator項(xiàng)目構(gòu)造中health端點(diǎn)工作原理
這篇文章主要介紹了springBoot-actuator中health端點(diǎn)工作原理,對spring-boot-actuator的項(xiàng)目構(gòu)造,工作原理進(jìn)行了全面的梳理,側(cè)重health健康檢查部分2022-02-02
Spring中使用copyProperties方法進(jìn)行對象之間的屬性賦值詳解
這篇文章主要介紹了Spring中使用copyProperties方法進(jìn)行對象之間的屬性賦值詳解,使用org.springframework.beans.BeanUtils.copyProperties方法進(jìn)行對象之間屬性的賦值,避免通過get、set方法一個(gè)一個(gè)屬性的賦值,需要的朋友可以參考下2023-12-12
jquery對輸入框內(nèi)容的數(shù)字校驗(yàn)代碼實(shí)例
這篇文章主要介紹了jquery對輸入框內(nèi)容的數(shù)字校驗(yàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
POI通用導(dǎo)出Excel(.xls,.xlsx)的方法
這篇文章主要介紹了POI通用導(dǎo)出Excel(.xls,.xlsx)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Java?8中讀取文件內(nèi)容?Files.lines()方法使用示例
這篇文章主要介紹了Java?8中讀取文件內(nèi)容Files.lines()方法如何使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
MyBatisPlus中事務(wù)處理的實(shí)現(xiàn)
本文主要介紹了MyBatisPlus中事務(wù)處理的實(shí)現(xiàn),包括事務(wù)的開啟、提交、回滾等操作,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07

