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
相关推荐
Humbunklung5 分钟前
Rust枚举:让数据类型告别单调乏味
开发语言·后端·rust
radient12 分钟前
Golang-GMP 万字洗髓经
后端·架构
蓝倾13 分钟前
如何使用API接口实现淘宝商品上下架监控?
前端·后端·api
舂春儿15 分钟前
如何快速统计项目代码行数
前端·后端
Pedantic15 分钟前
我们什么时候应该使用协议继承?——Swift 协议继承的应用与思
前端·后端
Codebee16 分钟前
如何利用OneCode注解驱动,快速训练一个私有的AI代码助手
前端·后端·面试
martinzh17 分钟前
用Spring AI搭建本地RAG系统:让AI成为你的私人文档助手
后端
MMJC621 分钟前
Playwright MCP Batch:革命性的批量自动化工具,让 Web 操作一气呵成
前端·后端·mcp
POLOAPI22 分钟前
Windows 系统安装与使用 Claude Code 全攻略
前端·后端
武子康23 分钟前
大数据-34 HBase 单节点配置 hbase-env hbase-site xml
大数据·后端·hbase