在Spring Boot中如何使用數據緩存
在實際開發(fā)中,對于要反復讀寫的數據,最好的處理方式是將之在內存中緩存一份,頻繁的數據庫訪問會造成程序效率低下,同時內存的讀寫速度本身就要強于硬盤。Spring在這一方面給我們提供了諸多的處理手段,而Spring Boot又將這些處理方式進一步簡化,接下來我們就來看看如何在Spring Boot中解決數據緩存問題。
創(chuàng)建Project并添加數據庫驅動
Spring Boot的創(chuàng)建方式還是和我們前文提到的創(chuàng)建方式一樣,不同的是這里選擇添加的依賴不同,這里我們添加Web、Cache和JPA依賴,如下圖:
創(chuàng)建成功之后,接下來添加數據庫驅動,我還是使用MySQL,在pom.xml中添加數據庫驅動,如下:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency>
配置application.properties
這個application.properties的配置還是和初識在Spring Boot中使用JPA一樣,各個參數的含義我這里也不再贅述,我們直接來看代碼:
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)建實體類
@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)建實體類的Repository
public interface PersonRepository extends JpaRepository<Person,Long> {}
創(chuàng)建業(yè)務類
業(yè)務接口
public interface DemoService {
public Person save(Person person);
public void remove(Long id);
public Person findOne(Person person);
}
實現(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() + "數據做了緩存");
return p;
}
@CacheEvict(value = "people")
@Override
public void remove(Long id) {
System.out.println("刪除了id、key為" + id + "的數據緩存");
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() + "數據做了緩存");
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() + "數據做了緩存");
return p;
}
@CacheEvict(value = "people")
@Override
public void remove(Long id) {
System.out.println("刪除了id、key為" + id + "的數據緩存");
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() + "數據做了緩存");
return p;
}
}
關于這個實現(xiàn)類我說如下幾點:
1.@CachePut表示緩存新添加的數據或者更新的數據到緩存中,兩個參數value表示緩存的名稱為people,key表示緩存的key為person的id
2.@CacheEvict表示從緩存people中刪除key為id的數據
3.@Cacheable表示添加數據到緩存中,緩存名稱為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,我們有三個地址要測試,一個一個來。當然,在 測試之前,我們先來看看初始狀態(tài)下的數據庫是什么樣子的:

首先我們在瀏覽器中訪問http://localhost:8080/able?id=1,得到如下訪問結果:
這個時候查看控制臺,輸出內容如下:
說是數據已經被緩存了,這個時候我們再繼續(xù)在瀏覽器中刷新繼續(xù)請求id為1的數據,會發(fā)現(xiàn)控制臺不會繼續(xù)打印日志出來,就是因為數據已被存于緩存之中了。
接下來我們向瀏覽器中輸入http://localhost:8080/put?age=47&name=奧巴牛&address=米國,訪問結果如下:
這個時候查看控制臺打印的日志如下:
再查看數據表,數據已插入成功:
此時,我們在瀏覽器中輸入http://localhost:8080/able?id=106,訪問剛剛插入的這條數據,結果如下:
這個時候查看控制臺,發(fā)現(xiàn)并沒有數據數據,就是因為數據已經處于緩存中了。
最后我們在瀏覽器中輸入http://localhost:8080/evit?id=106,將數據從緩存中移除,訪問結果如下:
這個時候查看控制臺,已經提示緩存移除掉了:
同時數據也從數據庫刪除掉了,這個時候如果還需要該數據則需要我們繼續(xù)向表中添加數據。
緩存技術切換
Spring Boot默認情況下使用ConcurrentMapCacheManager作為緩存技術,有的時候你可能想替換為其他的緩存方式,在Spring Boot中進行緩存的切換非常簡單,我這里以Google提供的Guava為例,如果要使用這種緩存策略,只需要添加相應的依賴即可,如下:
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>20.0</version> </dependency>
就這樣就可以了。實際上在Spring Boot中,底層使用哪一種緩存我們并不必做過多考慮,切換的方式也很簡單,如上文引入相應的依賴即可,我們只需要把上層的邏輯寫好即可。
本文案例下載:
本文GitHub地址https://github.com/lenve/JavaEETest/tree/master/Test25-Cache.
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Spring Boot Web 靜態(tài)文件緩存處理的方法
- spring boot+spring cache實現(xiàn)兩級緩存(redis+caffeine)
- Spring Boot 中使用cache緩存的方法
- SpringBoot項目中使用redis緩存的方法步驟
- 實例詳解Spring Boot實戰(zhàn)之Redis緩存登錄驗證碼
- Spring Boot緩存實戰(zhàn) EhCache示例
- Spring Boot 基于注解的 Redis 緩存使用詳解
- 詳解Spring Boot使用redis實現(xiàn)數據緩存
- Spring Boot集成Redis實現(xiàn)緩存機制(從零開始學Spring Boot)
- 在Spring Boot中實現(xiàn)HTTP緩存的方法
相關文章
Java 客戶端操作 FastDFS 實現(xiàn)文件上傳下載替換刪除功能
這篇文章主要介紹了Java 客戶端操作 FastDFS 實現(xiàn)文件上傳下載替換刪除功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
k8s部署springboot實現(xiàn)前后端分離項目
本文主要介紹了k8s部署springboot實現(xiàn)前后端分離項目,主要包括配置文件、鏡像構建和容器編排等方面,具有一定的參考價值,感興趣的可以了解一下2024-01-01
Java過濾器filter_動力節(jié)點Java學院整理
這篇文章主要介紹了Java過濾器filter,通過過濾器,可以對來自客戶端的請求進行攔截,進行預處理或者對最終響應給客戶端的數據進行處理后再輸出2017-07-07

