前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RedisTemplate保存二进制数据的方法

RedisTemplate保存二进制数据的方法

作者头像
方亮
发布2024-05-24 19:18:59
520
发布2024-05-24 19:18:59
举报
文章被收录于专栏:方亮方亮
大纲
  • 代码

在工作中,我们经常可以见到值是字符串类型的RedisTemplate<String, String>。而有些场景下,比如我们希望保存某个对象,而又不想使用外部的一些序列化方法,则可以考虑将数据保存而为二进制,然后保存到Redis中。这个时候,我们就可以使用Value类型是byte[]类型的RedisTemplate<String, byte[]>。 为了区分一般项目中常用的RedisTemplate<String, String>类型,我们给新的Bean取一个名字bytesRedisTemplate。

代码语言:javascript
复制
package org.example.redistemplateexample.config;

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;

@Configuration
public class RedisTemplateBeansConfig {

    @Bean(name = "bytesRedisTemplate")
    public RedisTemplate<String, byte[]> bytesRedisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, byte[]> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setValueSerializer(RedisSerializer.byteArray());
        redisTemplate.setHashValueSerializer(RedisSerializer.byteArray());
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

上述类会重用connectionFactory,这样就会和项目中其他默认的RedisTemplate使用的是相同的连接属性,包括地址、用户名、密码、连接池等。 然后定义一个封装了Redis操作的类

代码语言:javascript
复制
package org.example.redistemplateexample.redis;

import org.springframework.data.redis.core.RedisTemplate;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
@Component
public class BytesOperation {
    @Resource(name = "bytesRedisTemplate")
    private RedisTemplate<String, byte[]> redisTemplate;

    public void Set(String key, byte[] value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public byte[] Get(String key) {
        return redisTemplate.opsForValue().get(key);
    }
}

BytesOperation 类中的redisTemplate通过Resource的name指定为之前创建的Bean

在测试代码中,我们使用《使用java.io库序列化Java对象》中的序列化方法,将测试对象转换成二进制数组,然后保存到Redis中。

代码语言:javascript
复制
package org.example.redistemplateexample.redis;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.example.redistemplateexample.pojo.BaseTypes;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class BytesOperationTest {

    @Autowired
    private BytesOperation bytesOperation;

    @Test
    public void testSet() {
        BaseTypes baseTypesFrom = new BaseTypes();
        baseTypesFrom.setByteValue((byte) 1);
        baseTypesFrom.setByteValue((byte) 1);
        baseTypesFrom.setShortValue((short) 2);
        baseTypesFrom.setIntValue(3);
        baseTypesFrom.setLongValue(4L);
        baseTypesFrom.setFloatValue(5.0f);
        baseTypesFrom.setDoubleValue(6.0);
        baseTypesFrom.setCharValue('7');
        baseTypesFrom.setBooleanValue(true);

        String key = "baseTypes";

        try {
            bytesOperation.Set(key, serialize(baseTypesFrom));
        } catch (Exception e) {
            fail();
            throw new RuntimeException(e);
        }
        byte[] get = bytesOperation.Get(key);

        BaseTypes baseTypesTo = null;
        try {
            baseTypesTo = deserialize(get);
        } catch (Exception e) {
            fail();
            throw new RuntimeException(e);
        }
        assertEquals(baseTypesFrom, baseTypesTo);
    }

    public static <T> byte[] serialize(T obj) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
            oos.writeObject(obj);
            return bos.toByteArray();
        }
    }

    public static <T> T deserialize(byte[] data) throws Exception {
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        try( ObjectInputStream ois = new ObjectInputStream(bis)) {
            @SuppressWarnings("unchecked")
            T obj = (T) ois.readObject();  
            return obj;
        }
    }
}

代码

https://github.com/f304646673/RedisTemplateExample

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-05-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 大纲
  • 代码
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档