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
相关推荐
陌上丨2 小时前
Redis的Key和Value的设计原则有哪些?
数据库·redis·缓存
KYGALYX2 小时前
服务异步通信
开发语言·后端·微服务·ruby
AI_56782 小时前
AWS EC2新手入门:6步带你从零启动实例
大数据·数据库·人工智能·机器学习·aws
掘了2 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
ccecw2 小时前
Mysql ONLY_FULL_GROUP_BY模式详解、group by非查询字段报错
数据库·mysql
JH30732 小时前
达梦数据库与MySQL的核心差异解析:从特性到实践
数据库·mysql
数据知道2 小时前
PostgreSQL 核心原理:如何利用多核 CPU 加速大数据量扫描(并行查询)
数据库·postgresql
爬山算法2 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
猫头虎3 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
Moment3 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端