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
相关推荐
Java编程爱好者16 分钟前
手把手看懂 Java 字节码:讲透 Integer 判等、静态方法重写与 try-finally 核心底层
后端
Flittly18 分钟前
【AgentScope Java新手村系列】(2)第一个Agent-基础对话
java·spring boot·spring·ai
是发财不是旺财19 分钟前
Hermes 网关四层权限控制方案:让 AI Agent 安全地查数据库
数据库·安全·agent·openclaw·hermes
踏浪无痕23 分钟前
k8s发布服务,nacos未服务未下线紧急处理流程
后端
TYKJ02324 分钟前
物理安全:顶级机房为什么需要刷脸+指纹+工牌
后端
程序员黑豆30 分钟前
AI全栈开发 - Java:注释
前端·后端·ai编程
阿正的梦工坊34 分钟前
【Rust】04-借用、引用与切片
java·数据库·rust
AOwhisky39 分钟前
学习自测与解析:MySQL第五、六、七期核心知识点详解
运维·数据库·笔记·学习·mysql·云计算
小二·1 小时前
Spring Boot 3 + Vue 3 全栈开发实战
vue.js·spring boot·后端
阿标在干嘛1 小时前
政策平台的推送系统:消息队列、定时任务、AB测试的工程实践
服务器·数据库·ab测试