redis介紹
redis是目前業界使用最廣泛的內存數據存儲。相比memcached,redis支持更豐富的數據結構,例如hashes, lists, sets等,同時支持數據持久化。除此之外,redis還提供一些類數據庫的特性,比如事務,ha,主從庫??梢哉fredis兼具了緩存系統和數據庫的一些特性,因此有著豐富的應用場景。
引言
對于單元測試來說,我們應該讓它盡量保持單一環境,不要與網絡資源相通訊,這樣可以保證測試的穩定性與客觀性,對于springboot這個框架來說,它集成了單元測試junit,同時在設計項目時,你可以使用多種內嵌的存儲工具,像mongodb,redis,mysql等等,今天主要說一下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; /** * 實例化 redistemplate 對象 * * @return */ @bean public redistemplate<string, object> functiondomainredistemplate() { redistemplate<string, object> redistemplate = new redistemplate<>(); initdomainredistemplate(redistemplate, redisconnectionfactory); return redistemplate; } /** * 設置數據存入 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); } /** * 實例化 hashoperations 對象,可以使用 hash 類型操作 * * @param redistemplate * @return */ @bean public hashoperations<string, string, object> hashoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforhash(); } /** * 實例化 valueoperations 對象,可以使用 string 操作 * * @param redistemplate * @return */ @bean public valueoperations<string, object> valueoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforvalue(); } /** * 實例化 listoperations 對象,可以使用 list 操作 * * @param redistemplate * @return */ @bean public listoperations<string, object> listoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforlist(); } /** * 實例化 setoperations 對象,可以使用 set 操作 * * @param redistemplate * @return */ @bean public setoperations<string, object> setoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforset(); } /** * 實例化 zsetoperations 對象,可以使用 zset 操作 * * @param redistemplate * @return */ @bean public zsetoperations<string, object> zsetoperations(redistemplate<string, object> redistemplate) { return redistemplate.opsforzset(); } } |
在業務層中使用redis
1
2
|
@autowired redistemplate<string, object> rediscachetemplate; |
在使用過程中,我們的redistemplate對象已經被autowired注入了。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.cnblogs.com/lori/p/9104358.html