RedisTemplate序列化已实现存取对象

存取对象实现类(RedisTemplate操作封装)请去这里拿(https://blog.csdn.net/qq_39897814/article/details/86991991)已经实现存取对象

所需jar包

 

<!-- 工具库 -->
		<dependency>
		    <groupId>com.google.guava</groupId>
		    <artifactId>guava</artifactId>
		    <version>18.0</version>
		</dependency>
		<!-- 序列化 -->
		<dependency>
		    <groupId>com.dyuproject.protostuff</groupId>
		    <artifactId>protostuff-core</artifactId>
		    <version>1.1.3</version>
		</dependency>
		<dependency>
		    <groupId>com.dyuproject.protostuff</groupId>
		    <artifactId>protostuff-runtime</artifactId>
		    <version>1.1.3</version>
		</dependency>
<!-- spring-redis实现 -->
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>1.6.2.RELEASE</version>
		</dependency>
		<!-- redis客户端jar -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.8.0</version>
		</dependency>

代码实现 

package com.graduation.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;


/**
 * 序列化
 * @author 史俊杰
 *
 * 2019年3月6日
 */
public class SerializeUtil{
    
    
     /**
     * 序列化对象
     *
     * @param obj
     * @return
     */
    public static <T> byte[] serialize(T obj) { if (obj == null) { 
            throw new RuntimeException("Failed to serializer");
        }
        @SuppressWarnings("unchecked") 
        Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(obj.getClass());
        LinkedBuffer buffer = LinkedBuffer.allocate(1024 * 1024);
        byte[] protoStuff;
        try { protoStuff = ProtostuffIOUtil.toByteArray(obj, schema, buffer);
        } catch (Exception e) { 
            throw new RuntimeException("Failed to serializer");
        }finally { 
            buffer.clear();
        } 
        return protoStuff;
    } /**
     * 反序列化对象
     *
     * @param paramArrayOfByte
     * @param targetClass
     * @return
     */
    public static <T> T deserialize(byte[] paramArrayOfByte, Class<T> targetClass) { 
        if (paramArrayOfByte == null || paramArrayOfByte.length == 0) { 
            throw new RuntimeException("Failed to deserialize");
        } 
        T instance;
        try {
            instance = targetClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("Failed to deserialize");
        } 
        Schema<T> schema = RuntimeSchema.getSchema(targetClass);
        ProtostuffIOUtil.mergeFrom(paramArrayOfByte, instance, schema);
        return instance;
    } /**
     * 序列化列表
     *
     * @param objList
     * @return
     */
    public static <T> byte[] serializeList(List<T> objList) {
        if (objList == null || objList.isEmpty()) { 
            throw new RuntimeException("Failed to serializer");
        } @
        SuppressWarnings("unchecked") 
        Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(objList.get(0).getClass());
        LinkedBuffer buffer = LinkedBuffer.allocate(1024 * 1024);
        byte[] protoStuff;
        ByteArrayOutputStream bos = null;
        try { bos = new ByteArrayOutputStream();
            ProtostuffIOUtil.writeListTo(bos, objList, schema, buffer);
            protoStuff = bos.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException("Failed to serializer");
        } finally { buffer.clear();
            try { 
                if (bos != null) { 
                    bos.close();
                } 
            } catch (IOException e) {
                e.printStackTrace();
            } 
        
        } 
       return protoStuff;
    } /**
     * 反序列化列表
     *
     * @param paramArrayOfByte
     * @param targetClass
     * @return
     */
    public static <T> List<T> deserializeList(byte[] paramArrayOfByte, Class<T> targetClass) { 
        if (paramArrayOfByte == null || paramArrayOfByte.length == 0) { 
            throw new RuntimeException("Failed to deserialize");
        } 
        Schema<T> schema = RuntimeSchema.getSchema(targetClass);
        List<T> result;
        try { result = ProtostuffIOUtil.parseListFrom(new ByteArrayInputStream(paramArrayOfByte), schema);
        } catch (IOException e) { 
            throw new RuntimeException("Failed to deserialize");
        } return result;
    }
}


 

转载:https://blog.csdn.net/wsywb111/article/details/79612081 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值