当前位置: 移动技术网 > IT编程>开发语言>Java > spring boot中内嵌redis的使用方法示例

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

redis介绍

redis是目前业界使用最广泛的内存数据存储。相比memcached,redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化。除此之外,redis还提供一些类数据库的特性,比如事务,ha,主从库。可以说redis兼具了缓存系统和数据库的一些特性,因此有着丰富的应用场景。

引言

对于单元测试来说,我们应该让它尽量保持单一环境,不要与网络资源相通讯,这样可以保证测试的稳定性与客观性,对于springboot这个框架来说,它集成了单元测试junit,同时在设计项目时,你可以使用多种内嵌的存储工具,像mongodb,redis,mysql等等,今天主要说一下embedded-redis的使用。

使用方法如下:

添加包引用build.gradle

 testcompile(
   'com.github.kstyrc:embedded-redis:0.6'
 )

添加配置注入

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

 @autowired
 redistemplate<string, object> rediscachetemplate;

在使用过程中,我们的redistemplate对象已经被autowired注入了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网