redis介紹
redis是目前業(yè)界使用最廣泛的內(nèi)存數(shù)據(jù)存儲(chǔ)。相比memcached,redis支持更豐富的數(shù)據(jù)結(jié)構(gòu),例如hashes, lists, sets等,同時(shí)支持?jǐn)?shù)據(jù)持久化。除此之外,redis還提供一些類(lèi)數(shù)據(jù)庫(kù)的特性,比如事務(wù),ha,主從庫(kù)。可以說(shuō)redis兼具了緩存系統(tǒng)和數(shù)據(jù)庫(kù)的一些特性,因此有著豐富的應(yīng)用場(chǎng)景。
引言
對(duì)于單元測(cè)試來(lái)說(shuō),我們應(yīng)該讓它盡量保持單一環(huán)境,不要與網(wǎng)絡(luò)資源相通訊,這樣可以保證測(cè)試的穩(wěn)定性與客觀性,對(duì)于springboot這個(gè)框架來(lái)說(shuō),它集成了單元測(cè)試junit,同時(shí)在設(shè)計(jì)項(xiàng)目時(shí),你可以使用多種內(nèi)嵌的存儲(chǔ)工具,像mongodb,redis,mysql等等,今天主要說(shuō)一下embedded-redis的使用。
使用方法如下:
添加包引用build.gradle
1
2
3
|
testcompile( 'com.github.kstyrc:embedded-redis:0.6' ) |
添加配置注入
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
|
import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.data.redis.connection.redisconnectionfactory; import org.springframework.data.redis.core.hashoperations; import org.springframework.data.redis.core.listoperations; import org.springframework.data.redis.core.redistemplate; import org.springframework.data.redis.core.setoperations; import org.springframework.data.redis.core.valueoperations; import org.springframework.data.redis.core.zsetoperations; import org.springframework.data.redis.serializer.jdkserializationredisserializer; import org.springframework.data.redis.serializer.stringredisserializer; @configuration public class redisconfig { /** * 注入 redisconnectionfactory */ @autowired redisconnectionfactory redisconnectionfactory; /** * 實(shí)例化 redistemplate 對(duì)象 * * @return */ @bean public redistemplate<string, object> functiondomainredistemplate() { redistemplate<string, object> redistemplate = new redistemplate<>(); initdomainredistemplate(redistemplate, redisconnectionfactory); return redistemplate; } /** * 設(shè)置數(shù)據(jù)存入 redis 的序列化方式 * * @param redistemplate * @param factory */ private void initdomainredistemplate(redistemplate<string, object> redistemplate, redisconnectionfactory factory) { redistemplate.setkeyserializer( new stringredisserializer()); redistemplate.sethashkeyserializer( new stringredisserializer()); redistemplate.sethashvalueserializer( new jdkserializationredisserializer()); redistemplate.setvalueserializer( new jdkserializationredisserializer()); redistemplate.setconnectionfactory(factory); } /** * 實(shí)例化 hashoperations 對(duì)象,可以使用 hash 類(lèi)型操作 * * @param redistemplate * @return */ @bean public hashoperations<string, string, object> hashoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforhash(); } /** * 實(shí)例化 valueoperations 對(duì)象,可以使用 string 操作 * * @param redistemplate * @return */ @bean public valueoperations<string, object> valueoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforvalue(); } /** * 實(shí)例化 listoperations 對(duì)象,可以使用 list 操作 * * @param redistemplate * @return */ @bean public listoperations<string, object> listoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforlist(); } /** * 實(shí)例化 setoperations 對(duì)象,可以使用 set 操作 * * @param redistemplate * @return */ @bean public setoperations<string, object> setoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforset(); } /** * 實(shí)例化 zsetoperations 對(duì)象,可以使用 zset 操作 * * @param redistemplate * @return */ @bean public zsetoperations<string, object> zsetoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforzset(); } } |
在業(yè)務(wù)層中使用redis
1
2
|
@autowired redistemplate<string, object> rediscachetemplate; |
在使用過(guò)程中,我們的redistemplate對(duì)象已經(jīng)被autowired注入了。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:https://www.cnblogs.com/lori/p/9104358.html