一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - spring boot中內(nèi)嵌redis的使用方法示例

spring boot中內(nèi)嵌redis的使用方法示例

2021-05-08 12:16張占嶺 Java教程

這篇文章主要給大家介紹了關(guān)于spring boot中內(nèi)嵌redis使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日本一道高清二区三区 | bt伙计最新合集 | 婷婷色天使在线视频观看 | 久久免费看少妇级毛片蜜臀 | 精品一区二区国语对白 | 69欧美另类xxxxx高清 | 毛片大全高清免费 | 日本一区二区在线不卡 | 国产福利视频一区二区微拍视频 | 国产欧美视频高清va在线观看 | 青草青草伊人精品视频 | 91.prom在线观看国产 | 日本xxxxx高清免费观看 | 亚洲欧美精品一区天堂久久 | 嗯好爽视频 | 高清不卡日本v在线二区 | 国产亚洲一级精品久久 | 国产综合网站 | 亚洲10p | 东方影库四虎 | 毛片在线免费视频 | a级黄色网 | 99久久爱热6在线播放 | 好猛好紧好硬使劲好大刺激视频 | 欧美一级一级做性视频 | 韩国漂亮美女三级在线观看 | 91在线老师啪国自产 | 爽好大快深点一视频 | 四虎成人免费 | 成年人视频免费在线播放 | 国产高清在线精品一区二区 | 痴mu动漫成年动漫在线观看 | 欧美日韩精品一区二区三区视频播放 | 亚洲欧美国产精品完整版 | 人成网站在线观看 | 午夜欧美精品 | 免费特黄一区二区三区视频一 | 男男羞羞视频网站国产 | 国产成人精品免费视频软件 | 久久视频这有精品63在线国产 | a男人天堂 |