一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 在Spring Boot中如何使用數(shù)據(jù)緩存

在Spring Boot中如何使用數(shù)據(jù)緩存

2020-09-11 10:37_江南一點雨 Java教程

本篇文章主要介紹了在Spring Boot中如何使用數(shù)據(jù)緩存,具有一定的參考價值,有興趣的可以了解一下。

在實際開發(fā)中,對于要反復讀寫的數(shù)據(jù),最好的處理方式是將之在內(nèi)存中緩存一份,頻繁的數(shù)據(jù)庫訪問會造成程序效率低下,同時內(nèi)存的讀寫速度本身就要強于硬盤。spring在這一方面給我們提供了諸多的處理手段,而spring boot又將這些處理方式進一步簡化,接下來我們就來看看如何在spring boot中解決數(shù)據(jù)緩存問題。

創(chuàng)建project并添加數(shù)據(jù)庫驅(qū)動

spring boot的創(chuàng)建方式還是和我們前文提到的創(chuàng)建方式一樣,不同的是這里選擇添加的依賴不同,這里我們添加web、cache和jpa依賴,如下圖:

在Spring Boot中如何使用數(shù)據(jù)緩存

創(chuàng)建成功之后,接下來添加數(shù)據(jù)庫驅(qū)動,我還是使用mysql,在pom.xml中添加數(shù)據(jù)庫驅(qū)動,如下:

?
1
2
3
4
5
<dependency>
  <groupid>mysql</groupid>
  <artifactid>mysql-connector-java</artifactid>
  <version>5.1.40</version>
 </dependency>

配置application.properties

這個application.properties的配置還是和初識在spring boot中使用jpa一樣,各個參數(shù)的含義我這里也不再贅述,我們直接來看代碼:

?
1
2
3
4
5
6
7
8
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)建實體類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@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

?
1
public interface personrepository extends jparepository<person,long> {}

創(chuàng)建業(yè)務類

業(yè)務接口

?
1
2
3
4
5
6
7
public interface demoservice {
 public person save(person person);
 
 public void remove(long id);
 
 public person findone(person person);
}

實現(xiàn)類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@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;
 }
}

關于這個實現(xiàn)類我說如下幾點:

1.@cacheput表示緩存新添加的數(shù)據(jù)或者更新的數(shù)據(jù)到緩存中,兩個參數(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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@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)下的數(shù)據(jù)庫是什么樣子的:

在Spring Boot中如何使用數(shù)據(jù)緩存

首先我們在瀏覽器中訪問,得到如下訪問結(jié)果:

在Spring Boot中如何使用數(shù)據(jù)緩存

這個時候查看控制臺,輸出內(nèi)容如下:

在Spring Boot中如何使用數(shù)據(jù)緩存

說是數(shù)據(jù)已經(jīng)被緩存了,這個時候我們再繼續(xù)在瀏覽器中刷新繼續(xù)請求id為1的數(shù)據(jù),會發(fā)現(xiàn)控制臺不會繼續(xù)打印日志出來,就是因為數(shù)據(jù)已被存于緩存之中了。

接下來我們向瀏覽器中輸入http://localhost:8080/put?age=47&name=奧巴牛&address=米國,訪問結(jié)果如下:

在Spring Boot中如何使用數(shù)據(jù)緩存

這個時候查看控制臺打印的日志如下:

在Spring Boot中如何使用數(shù)據(jù)緩存

再查看數(shù)據(jù)表,數(shù)據(jù)已插入成功:

在Spring Boot中如何使用數(shù)據(jù)緩存

此時,我們在瀏覽器中輸入http://localhost:8080/able?id=106,訪問剛剛插入的這條數(shù)據(jù),結(jié)果如下:

在Spring Boot中如何使用數(shù)據(jù)緩存

這個時候查看控制臺,發(fā)現(xiàn)并沒有數(shù)據(jù)數(shù)據(jù),就是因為數(shù)據(jù)已經(jīng)處于緩存中了。

最后我們在瀏覽器中輸入http://localhost:8080/evit?id=106,將數(shù)據(jù)從緩存中移除,訪問結(jié)果如下:

在Spring Boot中如何使用數(shù)據(jù)緩存

這個時候查看控制臺,已經(jīng)提示緩存移除掉了:

在Spring Boot中如何使用數(shù)據(jù)緩存

同時數(shù)據(jù)也從數(shù)據(jù)庫刪除掉了,這個時候如果還需要該數(shù)據(jù)則需要我們繼續(xù)向表中添加數(shù)據(jù)。

緩存技術切換

spring boot默認情況下使用concurrentmapcachemanager作為緩存技術,有的時候你可能想替換為其他的緩存方式,在spring boot中進行緩存的切換非常簡單,我這里以google提供的guava為例,如果要使用這種緩存策略,只需要添加相應的依賴即可,如下:

?
1
2
3
4
5
<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.

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/u012702547/article/details/54142243

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 夫妻性生活在线 | 美女扒开腿让男人桶爽动态图片 | 果冻传媒九一制片厂 | dasd-698黑人在线播放 | 国产视频中文字幕 | 国产馆在线观看免费的 | 被教官揉了一晚上的奶小说 | 狠狠的撞进去嗯啊h女强男视频 | 日韩在线第一区 | 变态女王麻麻小说在线阅读 | 四虎影视库永久在线地址 | 婷婷色婷婷 | 视频一区 日韩 | 男人疯狂进女人下部视频动漫 | xxx中国bbbwww| 国产精品青青青高清在线密亚 | 久久伊人精品青青草原2021 | 无遮18禁在线永久免费观看挡 | 国产二区三区 | 四虎麻豆 | 国内久久久 | 91麻豆精品国产91久久久 | 92精品国产成人观看免费 | 天天中文| av中文字幕网免费观看 | 天天爱综合网 | 亚洲黄色天堂 | 美女精品永久福利在线 | 女同xx美女放 | 特黄特色大片免费视频播放 | 午夜亚洲视频 | 国产成人亚洲精品91专区手机 | 亚洲精品无码不卡在线观看 | 成人精品视频 成人影院 | 久久国产加勒比精品无码 | 国产日产精品久久久久快鸭 | 成年人在线视频观看 | jj视频免费看 | 男人捅女人动漫 | 日韩精品欧美高清区 | 无遮挡免费h肉动漫在线观看 |