Springboot集成Redis实例分析
来源:亿速云
时间:2024-04-23 18:15:10 136浏览 收藏
来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习数据库相关编程知识。下面本篇文章就来带大家聊聊《Springboot集成Redis实例分析》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!
依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
配置文件(application.properties)
# Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=x.x.x.x # Redis服务器连接端口 spring.redis.port=6738 # Redis服务器连接密码(默认为空) spring.redis.password= # 连接超时时间(毫秒) spring.redis.timeout=10000 # 连接池最大连接数(使用负值表示没有限制) spring.redis.jedis.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1ms # 连接池中的最大空闲连接 spring.redis.jedis.pool.max-idle=8 # 连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0
配置文件(RedisConfig.java)
package com.gxr.dmsData.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.text.SimpleDateFormat;
/**
* @author :gongxr
* @description: 自定义RedisTemplate
* @date :Created in 2021/6/30
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 修改key的默认序列化器为 string
RedisSerializer<String> stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setDefaultSerializer(stringRedisSerializer);
// 自定义 对象转换
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
Jackson2JsonRedisSerializer<Object> valueSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
valueSerializer.setObjectMapper(objectMapper);
// redisTemplate.setValueSerializer(valueSerializer);
// redisTemplate.setHashValueSerializer(valueSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}测试代码
import com.gxr.dmsData.common.BaseTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Set;
/**
* @author :gongxr
* @description:
* @date :Created in 2021/6/30
*/
@Slf4j
public class TestRedis extends BaseTest {
@Autowired
private RedisTemplate redisTemplate;
/**
* RedisTemplate中定义了对5种数据结构操作
* redisTemplate.opsForValue();//操作字符串
* redisTemplate.opsForHash();//操作hash
* redisTemplate.opsForList();//操作list
* redisTemplate.opsForSet();//操作set
* redisTemplate.opsForZSet();//操作有序set
*/
@Test
public void testRedisGet() {
String key = "adviceCalculateTime";
Boolean b = redisTemplate.hasKey(key);
log.info("key是否存在:{}", b);
Object o = redisTemplate.boundValueOps(key).get();
log.info(redisTemplate.toString());
log.info("查询结果:{}", o);
}
/**
* map类型
*/
@Test
public void testRedisHash() {
String key = "RRS_CURRENCY_CACHE";
Object o = redisTemplate.boundHashOps(key).get("590");
log.info("查询结果:{}", o.toString());
}
/**
* set类型
*/
@Test
public void testRedisSet() {
String key = "goodsDataSyncSkc:set";
Set set = redisTemplate.boundSetOps(key).members();
log.info("查询结果:{}", set.size());
String s = (String) redisTemplate.boundSetOps(key).randomMember();
log.info("查询结果:{}", s);
}
}好了,本文到此结束,带大家了解了《Springboot集成Redis实例分析》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多数据库知识!
声明:本文转载于:亿速云 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
418 收藏
-
403 收藏
-
112 收藏
-
252 收藏
-
302 收藏
-
325 收藏
-
157 收藏
-
257 收藏
-
398 收藏
-
232 收藏
-
283 收藏
-
141 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习