本文介绍: 优点:方便快捷存储java对象,只需要配置beanspring扫描内存开销较大,会存储多余的字节码;

Redis序列化操作java对象

1.前言
2.RedisTemplate的两种序列实践方案
3.方案一具体实现

1.依赖版本继承了SpringBoot版本

// maven依赖
<dependency&gt;
   <groupId&gt;org.springframework.boot</groupId&gt;
   <artifactId&gt;spring-boot-starter-data-redis</artifactId>
</dependency>

implementation "org.springframework.boot:spring-boot-starter-redis:1.3.5.RELEASE"

2.RedisConfig类

package com.cmft.ff.config.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;

/**
 * @Author: zeng_shuqi
 * @DateTime: 2023/3/28 10:00
 **/

@Configuration
public class RedisConfig {


    /**
     * redisTemplate 序列化使用jdkSerializeable, 存储二进制字节码, 所以自定义序列化类
     *
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 使用 GenericFastJsonRedisSerializer 替换默认序列化
        GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
        // 设置keyvalue的序列化规则
        redisTemplate.setKeySerializer(new GenericToStringSerializer<>(Object.class));
        redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);
        // 设置hashKey和hashValue的序列化规则
        redisTemplate.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
        redisTemplate.setHashValueSerializer(genericFastJsonRedisSerializer);
        // 设置支持事物
        redisTemplate.setEnableTransactionSupport(true);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

3.操作redis

@Service
@Slf4j
public class HuaFeiBoardClickhouseServiceImpl implements HuaFeiBoardClickhouseService {
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    public static final String KEY = "businessLineBO";
    @Override
    public BrokenBusinessLineBO getCountAndResponseTime() {
    	// 当key存在
		 if (!redisTemplate.hasKey(KEY)) {
		  // 逻辑代码 .....

		 // 将对象存储到reids当中key120秒过期  bo = value 代表你要存储的java对象
		  redisTemplate.opsForValue().set(KEY, bo, 120, TimeUnit.SECONDS);
		 }
		 // 当key存在时、根据key从redis获取数据  
		 return (BrokenBusinessLineBO) redisTemplate.opsForValue().get(KEY);
	}
    
}
4.方案二具体实现

1.如上一样需要配置依赖

在这里插入图片描述

@Service
@Slf4j
public class HuaFeiBoardClickhouseServiceImpl implements HuaFeiBoardClickhouseService {
   
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public static final String KEY = "businessLineBO";
    // spring使用的是这个大家可以使用自己熟悉的解析工具
    private final static ObjectMapper objectMapper = new ObjectMapper();
    @Override
    public BrokenBusinessLineBO getCountAndResponseTime() throws JsonProcessingException { // 这里抛出一个异常
    	// 当key不存在
		 if (!stringRedisTemplate.hasKey(KEY)) {
		   // 逻辑代码 .....
		   // 手动java对象序列化
            String json = objectMapper.writeValueAsString(bo);
            stringRedisTemplate.opsForValue().set(KEY, json, 120, TimeUnit.SECONDS);
            return bo;
		 }
		  // 获取key
        String json = stringRedisTemplate.opsForValue().get(KEY);
        // 反序列化
        BrokenBusinessLineBO businessLineBO = objectMapper.readValue(json, BrokenBusinessLineBO.class);
        return businessLineBO;
	}
    
}
5.总结

原文地址:https://blog.csdn.net/aq_money/article/details/129830071

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_39256.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注