国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring boot中內嵌redis的使用方法示例

spring boot中內嵌redis的使用方法示例

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

這篇文章主要給大家介紹了關于spring boot中內嵌redis使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

redis介紹

redis是目前業界使用最廣泛的內存數據存儲。相比memcached,redis支持更豐富的數據結構,例如hashes, lists, sets等,同時支持數據持久化。除此之外,redis還提供一些類數據庫的特性,比如事務,ha,主從庫。可以說redis兼具了緩存系統和數據庫的一些特性,因此有著豐富的應用場景。

引言

對于單元測試來說,我們應該讓它盡量保持單一環境,不要與網絡資源相通訊,這樣可以保證測試的穩定性與客觀性,對于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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本成人高清视频 | 午夜天 | 日韩精品一区二区三区在线观看 | 亚洲免费观看视频 | 色淫av | 成人黄色小视频 | 日韩不卡一区二区三区 | 98成人网| 依人在线免费视频 | 欧美 日韩 综合 | av在线一区二区 | 日韩精品免费视频 | 亚洲区视频 | 自拍偷拍第一页 | 亚洲精品日韩激情在线电影 | 一区二区久久 | 国产性猛交xxxx免费看久久 | 日韩国产精品一区二区 | 日本不卡免费新一二三区 | 成年无码av片在线 | 亚洲乱码国产乱码精品精98午夜 | 国产精品亚洲自拍 | 一区二区在线视频 | 欧美一级一 | 欧洲精品二区 | 涩涩涩久久久成人精品 | 欧洲成人| 色综合99| 成人片免费视频 | 日日夜夜av| 国产麻豆精品 | 黄网av| 中文字幕日产乱码六区小草 | 在线观看av网站永久 | 综合久| 999精品视频一区二区三区 | 日韩在线电影 | 亚洲 自拍 另类 欧美 丝袜 | 999精品一区 | 天操天天干 | 免费一级电影 |