RedisTemplate序列化已实现存取对象

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

所需jar包

 


		
		    com.google.guava
		    guava
		    18.0
		
		
		
		    com.dyuproject.protostuff
		    protostuff-core
		    1.1.3
		
		
		    com.dyuproject.protostuff
		    protostuff-runtime
		    1.1.3
		

		
			org.springframework.data
			spring-data-redis
			1.6.2.RELEASE
		
		
		
			redis.clients
			jedis
			2.8.0
		

代码实现 

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  byte[] serialize(T obj) { if (obj == null) { 
            throw new RuntimeException("Failed to serializer");
        }
        @SuppressWarnings("unchecked") 
        Schema schema = (Schema) 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 deserialize(byte[] paramArrayOfByte, Class 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 schema = RuntimeSchema.getSchema(targetClass);
        ProtostuffIOUtil.mergeFrom(paramArrayOfByte, instance, schema);
        return instance;
    } /**
     * 序列化列表
     *
     * @param objList
     * @return
     */
    public static  byte[] serializeList(List objList) {
        if (objList == null || objList.isEmpty()) { 
            throw new RuntimeException("Failed to serializer");
        } @
        SuppressWarnings("unchecked") 
        Schema schema = (Schema) 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  List deserializeList(byte[] paramArrayOfByte, Class targetClass) { 
        if (paramArrayOfByte == null || paramArrayOfByte.length == 0) { 
            throw new RuntimeException("Failed to deserialize");
        } 
        Schema schema = RuntimeSchema.getSchema(targetClass);
        List 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 

你可能感兴趣的:(RedisTemplate序列化已实现存取对象)