ABAP Netweaver, Hybris Commerce和SAP 云平臺的登錄認證
1067
2025-04-01
Spring從3.1開始定義了org.springframework.cache.Cache和org.springframework.cache.CacheManager 接口來統一不同的緩存技術;并支持使用JCache(JSR-107)注解簡化我們開發;Cache接口為緩存的組件規范定義,包含緩存的各種操作集合;Cache接口下Spring提供了各種xxxCache的實現;如RedisCache,EhCacheCache ,ConcurrentMapCache等;本文我們就來介紹下SpringCache的具體使用。
一、緩存中的重要概念
@Cacheable/@CachePut/@CacheEvict 主要的參數
二、SpEL上下文數據
Spring Cache提供了一些供我們使用的SpEL上下文數據,下表直接摘自Spring官方文檔
注意:
當我們要使用root對象的屬性作為key時我們也可以將“#root”省略,因為Spring默認使用的就是root對象的屬性。 如
@Cacheable(key = “targetClass + methodName +#p0”)
使用方法參數時我們可以直接使用“#參數名”或者“#p參數index”。 如:
@Cacheable(value=“users”, key="#id")
@Cacheable(value=“users”, key="#p0")
三、SpringCache的使用
1.導入依賴
1
2
3
4
2.然后在啟動類注解@EnableCaching開啟緩存
3.創建業務類
package com.dpb.springboot.service; import com.dpb.springboot.pojo.User; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; /** * @program: springboot-13-cache * @description: * @author: 波波烤鴨 * @create: 2019-11-27 21:25 */ @Service public class UserService { /** * @Cacheable注解會先查詢是否已經有緩存,有會使用緩存,沒有則會執行方法并緩存 * 此處的User實體類一定要實現序列化public class User implements Serializable,否則會報java.io.NotSerializableException異常。 * @param userName * @return */ @Cacheable(value = "userCache" , key = "#userName") public User getUserByName(String userName){ System.out.println("數據庫查詢...." + userName); return getFromDB(userName); } /** * 清除一條記錄 * @param user */ @CacheEvict(value = "userCache",key = "#user.name") public void updateUser(User user){ System.out.println("數據更新了。。。。數據庫"); updateDB(user); } /** * allEntries:是否清空所有緩存內容,缺省為 false,如果指定為 true,則方法調用后將立即清空所有緩存 * beforeInvocation:是否在方法執行前就清空,缺省為 false,如果指定為 true, * 則在方法還沒有執行的時候就清空緩存,缺省情況下,如果方法執行拋出異常,則不會清空緩存 */ @CacheEvict(value = "userCache",allEntries = true,beforeInvocation = true) public void reload(){ // } private User getFromDB(String userName){ System.out.println("查詢數據庫..." + userName); return new User(666,userName); } private void updateDB(User user){ System.out.println("更新數據..." + user.getName()); } }
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
56
57
4.創建緩存實現類
我們自定義一個基于內存的緩存實現 Cache接口,并實現相關的方法
package com.dpb.springboot.cache; import org.springframework.cache.Cache; import org.springframework.cache.support.SimpleValueWrapper; import java.util.Map; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; /** * @program: springboot-13-cache * @description: * @author: 波波烤鴨 * @create: 2019-11-27 21:32 */ public class MyCache implements Cache { // 緩存的 key private String name; // 保存緩存數據的容器 private Map
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
5.配置緩存管理器
Spring的配置文件中如下配置
6.測試代碼
package com.dpb.springboot; import com.dpb.springboot.pojo.User; import com.dpb.springboot.service.UserService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class Springboot13CacheApplicationTests { @Autowired private UserService service ; @Test void contextLoads() { System.out.println("第一次查詢..."); service.getUserByName("hello"); System.out.println("第二次查詢..."); service.getUserByName("hello"); System.out.println("*************"); // 更新記錄 User user1 = service.getUserByName("user1"); // 開始更新其中一個 user1.setId(1111); service.updateUser(user1); // 更新后再查詢 service.getUserByName("user1"); // 再次查詢 service.getUserByName("user1"); // 更新所有 service.reload(); System.out.println("清空了所有的緩存..."); service.getUserByName("user1"); service.getUserByName("user2"); service.getUserByName("user1"); service.getUserByName("user2"); } }
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
測試結果
第一次查詢... 執行了緩存查詢...沒有命中hello 數據庫查詢....hello 查詢數據庫...hello 數據緩存了...hello 第二次查詢... 執行了緩存查詢...命中hello ************* 執行了緩存查詢...沒有命中user1 數據庫查詢....user1 查詢數據庫...user1 數據緩存了...user1 數據更新了。。。。數據庫 更新數據...user1 移走了元素:user1 執行了緩存查詢...沒有命中user1 數據庫查詢....user1 查詢數據庫...user1 數據緩存了...user1 執行了緩存查詢...命中user1 清空了所有的緩存... 執行了緩存查詢...沒有命中user1 數據庫查詢....user1 查詢數據庫...user1 數據緩存了...user1 執行了緩存查詢...沒有命中user2 數據庫查詢....user2 查詢數據庫...user2 數據緩存了...user2 執行了緩存查詢...命中user1 執行了緩存查詢...命中user2
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
搞定~
Spring 數據庫
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。