激情久久久_欧美视频区_成人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,主從庫??梢哉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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久精品人人做人人爽 | 久久精品亚洲一区二区三区观看模式 | 在线一级片 | 国产1区2| 成年人高清视频在线观看 | 激情小说激情图片激情电影 | 98色视频| 特级毛片免费视频 | 国产精品亚洲激情 | 黄色av免费网站 | 人成免费a级毛片 | 久久亚洲精品久久国产一区二区 | 久草成人在线观看 | 欧美成人高清视频 | 日本成人午夜 | 九九精品在线观看视频 | 亚洲精品久久久久久久久久久 | 国产在线观看一区二区三区 | 久久综合九色 | 欧日韩| 午夜精品在线视频 | 欧美色大成网站www永久男同 | 日韩精品久久一区二区三区 | 国内久久久久 | 国产精品美女久久久久久不卡 | 一区二区久久久久草草 | 欧美一级黄色片免费观看 | 国产成人小视频在线观看 | 亚洲国产精品二区 | 热久久成人 | 暴力强行进如hdxxx | 久久久成人精品视频 | 12av毛片 | 深夜毛片免费看 | 午夜国产成人 | 成人444kkkk在线观看 | 一区二区三区四区在线观看视频 | 91久久另类重口变态 | 日韩视频在线不卡 | 国产资源在线观看视频 | 亚洲免费视频一区二区 |