服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

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

延伸 · 阅读

精彩推荐