Springboot集成Redis实例分析
作者:小月亮
时间:2023-04-25
来源:互联网
浏览:0
依赖包org.springframework.bootspring-boot-starter-data-redis配置文件(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.
依赖包
org.springframework.boot spring-boot-starter-data-redis
配置文件(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测试代码
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);
}
}
作者最新文章
纯纯写作
2026-09-16 17:42
JMeter入门:创建HTTP请求并验证响应结果
2026-09-02 10:20
文件表格制作教程:选择Word或Excel的判断方法
2026-09-02 09:45
多个PPT怎么一次性转PDF?PPT批量转换工具有哪些?
2026-09-02 06:00
PDF图纸转CAD的3种方法及比例校准指南
2026-09-01 18:36
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















