數(shù)據(jù)緩存原因:有些數(shù)據(jù)比較多,如果每次訪問(wèn)都要進(jìn)行查詢,無(wú)疑給數(shù)據(jù)庫(kù)帶來(lái)太大的負(fù)擔(dān),將一些龐大的查詢數(shù)據(jù)并且更新次數(shù)較少的數(shù)據(jù)存入redis,能為系統(tǒng)的性能帶來(lái)良好的提升。
業(yè)務(wù)邏輯思路:登入系統(tǒng),訪問(wèn)數(shù)據(jù)時(shí),檢查redis是否有緩存,有則直接從redis中提取,沒(méi)有則從數(shù)據(jù)庫(kù)查詢出,并存入redis中做緩存。
為什么要用redis做緩存:
(1)異常快速:redis的速度非常快,每秒能執(zhí)行約11萬(wàn)集合,每秒約81000+條記錄。
(2)支持豐富的數(shù)據(jù)類型:redis支持最大多數(shù)開發(fā)人員已經(jīng)知道像列表,集合,有序集合,散列數(shù)據(jù)類型。這使得它非常容易解決各種各樣的問(wèn)題,因?yàn)槲覀冎滥男﹩?wèn)題是可以處理通過(guò)它的數(shù)據(jù)類型更好。
(3)操作都是原子性:所有redis操作是原子的,這保證了如果兩個(gè)客戶端同時(shí)訪問(wèn)的redis服務(wù)器將獲得更新后的值。
(4)多功能實(shí)用工具:redis是一個(gè)多實(shí)用的工具,可以在多個(gè)用例如緩存,消息,隊(duì)列使用(redis原生支持發(fā)布/訂閱),任何短暫的數(shù)據(jù),應(yīng)用程序,如web應(yīng)用程序會(huì)話,網(wǎng)頁(yè)命中計(jì)數(shù)等。
緩存實(shí)現(xiàn)思路:
- 項(xiàng)目中配置好redis賬戶等屬性文件(redis.properties)
- 整合到spring容器中(application-redis.xml)
- 編寫redis工具類
一、項(xiàng)目中配置好redis賬戶等屬性文件(redis.properties)
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
|
#ip地址 redis.hostname=youripaddress #端口號(hào) redis.port= 6379 #如果有密碼 redis.password=yourredispassword #客戶端超時(shí)時(shí)間單位是毫秒 默認(rèn)是 2000 redis.timeout= 10000 #最大空閑數(shù) redis.maxidle= 300 #連接池的最大數(shù)據(jù)庫(kù)連接數(shù)。設(shè)為 0 表示無(wú)限制,如果是jedis 2.4 以后用redis.maxtotal #redis.maxactive= 600 #控制一個(gè)pool可分配多少個(gè)jedis實(shí)例,用來(lái)替換上面的redis.maxactive,如果是jedis 2.4 以后用該屬性 redis.maxtotal= 1000 #最大建立連接等待時(shí)間。如果超過(guò)此時(shí)間將接到異常。設(shè)為- 1 表示無(wú)限制。 redis.maxwaitmillis= 1000 #連接的最小空閑時(shí)間 默認(rèn) 1800000 毫秒( 30 分鐘) redis.minevictableidletimemillis= 300000 #每次釋放連接的最大數(shù)目,默認(rèn) 3 redis.numtestsperevictionrun= 1024 #逐出掃描的時(shí)間間隔(毫秒) 如果為負(fù)數(shù),則不運(yùn)行逐出線程, 默認(rèn)- 1 redis.timebetweenevictionrunsmillis= 30000 #是否在從池中取出連接前進(jìn)行檢驗(yàn),如果檢驗(yàn)失敗,則從池中去除連接并嘗試取出另一個(gè) redis.testonborrow= true #在空閑時(shí)檢查有效性, 默認(rèn) false redis.testwhileidle= true |
二、整合到spring容器中(application-redis.xml)
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
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "http://www.springframework.org/schema/context" xmlns:mvc= "http://www.springframework.org/schema/mvc" xmlns:cache= "http://www.springframework.org/schema/cache" xmlns:aop= "http://www.springframework.org/schema/aop" xsi:schemalocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context.xsd http: //www.springframework.org/schema/mvc http: //www.springframework.org/schema/mvc/spring-mvc.xsd http: //www.springframework.org/schema/cache http: //www.springframework.org/schema/cache/spring-cache.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 加載配置文件 --> <context:property-placeholder ignore-unresolvable= "true" location= "classpath:properties/redis.properties" /> <!-- redis連接池配置--> <bean id= "jedispoolconfig" class = "redis.clients.jedis.jedispoolconfig" > <!--最大空閑數(shù)--> <property name= "maxidle" value= "${redis.maxidle}" /> <!--連接池的最大數(shù)據(jù)庫(kù)連接數(shù) --> <property name= "maxtotal" value= "${redis.maxtotal}" /> <!--最大建立連接等待時(shí)間--> <property name= "maxwaitmillis" value= "${redis.maxwaitmillis}" /> <!--逐出連接的最小空閑時(shí)間 默認(rèn) 1800000 毫秒( 30 分鐘)--> <property name= "minevictableidletimemillis" value= "${redis.minevictableidletimemillis}" /> <!--每次逐出檢查時(shí) 逐出的最大數(shù)目 如果為負(fù)數(shù)就是 : 1 /abs(n), 默認(rèn) 3 --> <property name= "numtestsperevictionrun" value= "${redis.numtestsperevictionrun}" /> <!--逐出掃描的時(shí)間間隔(毫秒) 如果為負(fù)數(shù),則不運(yùn)行逐出線程, 默認(rèn)- 1 --> <property name= "timebetweenevictionrunsmillis" value= "${redis.timebetweenevictionrunsmillis}" /> <!--是否在從池中取出連接前進(jìn)行檢驗(yàn),如果檢驗(yàn)失敗,則從池中去除連接并嘗試取出另一個(gè)--> <property name= "testonborrow" value= "${redis.testonborrow}" /> <!--在空閑時(shí)檢查有效性, 默認(rèn) false --> <property name= "testwhileidle" value= "${redis.testwhileidle}" /> </bean > <!--redis連接工廠 --> <bean id= "jedisconnectionfactory" class = "org.springframework.data.redis.connection.jedis.jedisconnectionfactory" destroy-method= "destroy" > <property name= "poolconfig" ref= "jedispoolconfig" ></property> <!--ip地址 --> <property name= "hostname" value= "${redis.hostname}" ></property> <!--端口號(hào) --> <property name= "port" value= "${redis.port}" ></property> <!--如果redis設(shè)置有密碼 --> <property name= "password" value= "${redis.password}" /> <!--客戶端超時(shí)時(shí)間單位是毫秒 --> <property name= "timeout" value= "${redis.timeout}" ></property> </bean> <!--redis操作模版,使用該對(duì)象可以操作redis --> <bean id= "redistemplate" class = "org.springframework.data.redis.core.redistemplate" > <property name= "connectionfactory" ref= "jedisconnectionfactory" /> <!--如果不配置serializer,那么存儲(chǔ)的時(shí)候缺省使用string,如果用user類型存儲(chǔ),那么會(huì)提示錯(cuò)誤user can't cast to string!! --> <property name= "keyserializer" > <bean class = "org.springframework.data.redis.serializer.stringredisserializer" /> </property> <property name= "valueserializer" > <bean class = "org.springframework.data.redis.serializer.genericjackson2jsonredisserializer" /> </property> <property name= "hashkeyserializer" > <bean class = "org.springframework.data.redis.serializer.stringredisserializer" /> </property> <property name= "hashvalueserializer" > <bean class = "org.springframework.data.redis.serializer.genericjackson2jsonredisserializer" /> </property> <!--開啟事務(wù) --> <property name= "enabletransactionsupport" value= "true" ></property> </bean > <!--自定義redis工具類,在需要緩存的地方注入此類 --> <bean id= "redisutil" class = "com.neuedu.crm.utils.redisutil" > <property name= "redistemplate" ref= "redistemplate" /> </bean> </beans> |
三、編寫redis工具類
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
|
package com.neuedu.crm.utils; import java.io.serializable; import java.util.set; import java.util.concurrent.timeunit; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.data.redis.core.redistemplate; import org.springframework.data.redis.core.valueoperations; /** * redis工具類 * :用于緩存數(shù)據(jù) * */ public class redisutil { private logger logger = loggerfactory.getlogger(redisutil. class ); private redistemplate<serializable, object> redistemplate; public void setredistemplate(redistemplate<serializable, object> redistemplate) { this .redistemplate = redistemplate; } /** * 批量刪除對(duì)應(yīng)的value * * @param keys */ public void remove( final string... keys) { for (string key : keys) { remove(key); } } /** * 批量刪除key * * @param pattern */ public void removepattern( final string pattern) { set<serializable> keys = redistemplate.keys(pattern); if (keys.size() > 0 ) { redistemplate.delete(keys); } } /** * 刪除對(duì)應(yīng)的value * * @param key */ public void remove( final string key) { logger.info( "要移除的key為:" + key); if (exists(key)) { redistemplate.delete(key); } } /** * 判斷緩存中是否有對(duì)應(yīng)的value * * @param key * @return */ public boolean exists( final string key) { logger.info( "要驗(yàn)證是否存在的key為:" + key); return redistemplate.haskey(key); } /** * 讀取緩存 * * @param key * @return */ public object get( final string key) { object result = null ; valueoperations<serializable, object> operations = redistemplate .opsforvalue(); result = operations.get(key); return result; } /** * 寫入緩存 * * @param key * @param value * @return */ public boolean set( final string key, object value) { boolean result = false ; try { valueoperations<serializable, object> operations = redistemplate .opsforvalue(); operations.set(key, value); result = true ; } catch (exception e) { logger.error( "系統(tǒng)異常" ,e); } return result; } /** * 寫入緩存 * * @param key * @param value * @return */ public boolean set( final string key, object value, long expiretime) { boolean result = false ; try { valueoperations<serializable, object> operations = redistemplate .opsforvalue(); operations.set(key, value); redistemplate.expire(key, expiretime, timeunit.seconds); result = true ; } catch (exception e) { logger.error( "系統(tǒng)異常" ,e); } return result; } } |
注意點(diǎn):redis工具類由spring進(jìn)行托管,則在需要緩存的地方注入redis工具類即可。
總結(jié)
以上所述是小編給大家介紹的spring整合redis實(shí)現(xiàn)數(shù)據(jù)緩存的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
原文鏈接:https://blog.csdn.net/chenjianzhou107/article/details/82724947