依賴如下:
1
2
3
4
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency> |
配置文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
spring: redis: open: true # 是否開(kāi)啟redis緩存 true 開(kāi)啟 false 關(guān)閉 database: 0 host: 47.104 . 208.124 port: 6378 password: lf. 1228 # 密碼(默認(rèn)為空) timeout: 6000 # 連接超時(shí)時(shí)長(zhǎng)(毫秒) pool: max-active: 1000 # 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制) max-wait: - 1 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制) max-idle: 10 # 連接池中的最大空閑連接 min-idle: 5 # 連接池中的最小空閑連接 |
redisconfig類:
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
|
@configuration public class redisconfig { @autowired private redisconnectionfactory factory; @bean public redistemplate<string, object> redistemplate() { redistemplate<string, object> redistemplate = new redistemplate<>(); redistemplate.setkeyserializer( new stringredisserializer()); redistemplate.sethashkeyserializer( new stringredisserializer()); redistemplate.sethashvalueserializer( new stringredisserializer()); redistemplate.setvalueserializer( new stringredisserializer()); redistemplate.setconnectionfactory(factory); return redistemplate; } @bean public hashoperations<string, string, object> hashoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforhash(); } @bean public valueoperations<string, string> valueoperations(redistemplate<string, string> redistemplate) { return redistemplate.opsforvalue(); } @bean public listoperations<string, object> listoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforlist(); } @bean public setoperations<string, object> setoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforset(); } @bean public zsetoperations<string, object> zsetoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforzset(); } } |
redisutils如下:
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
|
@component public class redisutils { @autowired private redistemplate<string, object> redistemplate; @autowired private valueoperations<string, string> valueoperations; @autowired private hashoperations<string, string, object> hashoperations; @autowired private listoperations<string, object> listoperations; @autowired private setoperations<string, object> setoperations; @autowired private zsetoperations<string, object> zsetoperations; /** 默認(rèn)過(guò)期時(shí)長(zhǎng),單位:秒 */ public final static long default_expire = 60 * 60 * 24 ; /** 不設(shè)置過(guò)期時(shí)長(zhǎng) */ public final static long not_expire = - 1 ; private final static gson gson = new gson(); public void set(string key, object value, long expire){ valueoperations.set(key, tojson(value)); if (expire != not_expire){ redistemplate.expire(key, expire, timeunit.seconds); } } public void set(string key, object value){ set(key, value, default_expire); } public <t> t get(string key, class <t> clazz, long expire) { string value = valueoperations.get(key); if (expire != not_expire){ redistemplate.expire(key, expire, timeunit.seconds); } return value == null ? null : fromjson(value, clazz); } public <t> t get(string key, class <t> clazz) { return get(key, clazz, not_expire); } public string get(string key, long expire) { string value = valueoperations.get(key); if (expire != not_expire){ redistemplate.expire(key, expire, timeunit.seconds); } return value; } public string get(string key) { return get(key, not_expire); } public void delete(string key) { redistemplate.delete(key); } /** * object轉(zhuǎn)成json數(shù)據(jù) */ private string tojson(object object){ if (object instanceof integer || object instanceof long || object instanceof float || object instanceof double || object instanceof boolean || object instanceof string){ return string.valueof(object); } return gson.tojson(object); } /** * json數(shù)據(jù),轉(zhuǎn)成object */ private <t> t fromjson(string json, class <t> clazz){ return gson.fromjson(json, clazz); } } |
springboot如何封裝redis:
redistemplate
所在包: org.springframework.data.redis.core
作用:redis模板,redis事務(wù),序列化支持,操作redis方法
jedisconnectionfactory
所在包:org.springframework.data.redis.connection.jedis
作用:redis連接工廠類,創(chuàng)建redis連接池等
redisautoconfiguration
所在包:org.springframework.boot.autoconfigure.data.redis
作用:將redis配置文件相關(guān)信息注入工廠類
redisproperties
所在包:org.springframework.boot.autoconfigure.data.redis
作用:redis連接基礎(chǔ)類通過(guò)@configurationproperties注解將配置信息注入屬性
總結(jié)
以上所述是小編給大家介紹的redis在springboot中的使用教程,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://blog.csdn.net/qq_39533847/article/details/80680873