SpringBoot项目Redis使用

SpringBoot项目Redis使用

引入依赖

xml 复制代码
<!-- redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

对Redis的信息进行配置

yaml 复制代码
  redis:
    database: 0 # 使用Redis的哪一个数据库,0-15 任选
    port: 6379 # 自己的Redis配置的端口
    host: localhost # 自己Redis的网络自己,我这里是本地
    password: xxxxxxx # 自己Redis配置的密码
    lettuce: # 关于lettuce这个默认也行
      pool:
        min-idle: 1
        max-active: 10
        max-idle: 10

为方便使用设置序列化器,使用时统一用RedisTemplate<String,Object>模式

这里就是注册一个Redis序列化的Bean方便使用

java 复制代码
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        // 我们为了自己开发方便,一般直接使用 <String, Object>
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(redisConnectionFactory);
        // Json序列化配置
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // String 的序列化
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

到这里基本就可以使用了

使用

我这里进行测试使用

使用key:string-value:string模式

首先直接使用一般的设置Value,这里的代码就是设置一对键值

java 复制代码
@SpringBootTest
class SpringbootTemplateApplicationTests {

	@Resource
	RedisTemplate<String,Object> redisTemplate ;
	@Test
	void testRedis(){
		String phone = "12345678901";
		redisTemplate.opsForValue().set("test:code:"+ phone,"123456");
 		System.out.println(redisTemplate.opsForValue().get("test:code:"+phone));
	}
}

结果:

用图形化工具查看

符合预期的结果

使用Hash模式

使用代码:

java 复制代码
@Test
	void testRedisHashValue(){
		String token = UUID.randomUUID().toString();
		User user = new User(1235432l,"xwhking","password","xiaoxiaowei");
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(user);
		redisTemplate.opsForHash().putAll("test:user:"+token,stringObjectMap);
		System.out.println(redisTemplate.opsForHash().entries("test:user:"+token));
	}

打印结果:

图形化结果查看:

直接存一个一个对象呢?会转化为JSON进行存储

只不过对这里取出来的值进行使用的时候要进行强制类型转换

使用代码:

java 复制代码
void testRedisJson(){
		String token = UUID.randomUUID().toString();
		User user = new User(1235432l,"xwhking","password","xiaoxiaowei");
		redisTemplate.opsForValue().set("test:user:"+token,user);
		System.out.println(redisTemplate.opsForValue().get("test:user:"+token));  
	}

运行结果:

图形化结果:

相关推荐
高频因子挖掘机32 分钟前
只取最近 60 个交易日,却每次拉全历史:量化脚本变慢的原因与改法
后端·github·api
孟健1 小时前
从5000亿Token到83%字节匹配:AI逆向工程与重构的工程闭环实践
后端·架构·ai编程
paopaokaka_luck2 小时前
滑雪场管理系统(AI智能助手流式对话,ECharts 运营数据分析、教练、雪具与场地一体化预约,钱包支付退款、订单核销与服务评价)
java·人工智能·spring boot·网络协议·数据分析·echarts
SL_staff2 小时前
JVS混合架构实践:能力引擎+动态模型如何支撑企业级可持续交付
spring boot·低代码·全栈
码事漫谈2 小时前
丢得静悄悄,谈一谈 Agent 的记忆是怎么没的
后端
迪飞特科技2 小时前
智慧物业平台实战:Spring Boot 整合小程序实现报修巡检费用全流程
spring boot·后端·小程序
考虑考虑2 小时前
@JsonInclude注解使用
spring boot·后端·spring
IT_陈寒3 小时前
Vite的这个问题坑了我三天,原来如此解决!
前端·人工智能·后端
网易云信3 小时前
大模型能上岗做销售吗?我们让 12 款国产模型交了 420 份作业
前端·后端
CCblandy3 小时前
HTTP 长连接 3 类故障实测:[WinError 10053]、99 个 TIME_WAIT、69 次多余握手,4 种对策怎么选
后端·python·网络协议·http·云原生·性能优化·架构