nosql--RedisTemplate定制化

nosql--RedisTemplate定制化

1、序列化

stringRedisTemplate

RedisTemplate<Object,Object> redisTemplate

2、如果使用redis中保存数据会使用默认的序列化机制,导致redis中保存的对象不可视

2.1将所有的对象以JSON的形式保存

默认序列化是JdkSerializationRedisSerializer

如果没有redisTemplate配置

2.2配置reids自定义配置

2.3转化成功

2.4配置文件代码

java 复制代码
package com.example.boot3.redis.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

/**
 * @author jitwxs
 * @date 2023年12月25日 22:23
 */
public class AppTedisConfiguration {
   /*
   允许Object类型的key-value,都可以转换成json进行存储
   @param redisConnecctionFactory 自动配置好连接工厂
   @return
   */
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
//        把对象转为json字符串的序列化工具
        template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}

3redis客户端

RedisTemplate、StringRedisTemplae:操作redis的工具类

要从redis的连接厂获取连接才能操作redis

Redis 客户端:

jedis

Lettuce:默认

配置jedis、lettuce的底层参数

java 复制代码
#设置lettuce的底层参数
#spring.data.redis.client-type=lettuce
#spring.data.redis.lettuce.pool.enabled=true
##线程池
#spring.data.redis.lettuce.pool.max-active=8

#设置jedis的底层参数
spring.data.redis.client-type=jedis
spring.data.redis.jedis.pool.enabled=true
spring.data.redis.jedis.pool.max-active=8
相关推荐
Scene2168 分钟前
AgentScope 2.0:2. 快速上手 从零构建生产级智能体
后端
前端一课14 分钟前
用 TRAE Work 把项目踩坑经验沉淀成「团队可复用工程规范」,新人再也不重复掉坑
前端·后端
神奇小汤圆22 分钟前
一文吃透 Spring 框架:原理、实践与面试全解析
后端
站大爷IP24 分钟前
Python 的切片把我坑惨了,原来 `[:]` 是浅拷贝,而 `copy.deepcopy` 才是我的救命稻草
后端
神奇小汤圆26 分钟前
Java 万字长文:从零基础到高级应用的完整教程——把面向对象讲透
后端
孓最求完美28 分钟前
实战经验:JT808/JT809 车联网高并发服务端性能优化指南
后端
SelectDB31 分钟前
Apache Doris AI RAG 实战:从基础 RAG 到知识图谱增强的技术能力与选型
数据库
shengjk132 分钟前
深度拆解:从 LC-3 汇编到 Java/Rust,数组越界与硬件中断的底层真相
后端
Gorway34 分钟前
理解 Spring 依赖注入:从构造器注入到集合与条件 Bean
java·后端