redis数据(包含porotobuf类)的序列化问题

  • 基本上根据我这两个文件就可以解决所有redis序列化的问题

1.序列化配置文件

public class RobotRedisSerializer<T> implements RedisSerializer<T> {


    private FastJsonConfig fastJsonConfig = new FastJsonConfig();
    @SuppressWarnings("unused")
    private Class<T> type;

    public FastJsonConfig getFastJsonConfig() {
        return fastJsonConfig;
    }

    public void setFastJsonConfig(FastJsonConfig fastJsonConfig) {
        this.fastJsonConfig = fastJsonConfig;
    }

    public void ProtocAndJSONRedisSerialize(Class<T> type) {
        this.type = type;
    }

    @Override
    public byte[] serialize(T t) throws SerializationException {
            ObjectOutputStream oos = null;
            ByteArrayOutputStream baos = null;
        try {
                if(t instanceof GeneratedMessageV3){
                    byte [] bytes = ((Issue) t).toByteArray();
                    byte [] bytes2={0};
                    byte [] bytes3 = new byte[bytes.length + bytes2.length];
                    System.arraycopy(bytes, 0, bytes3, 0, bytes.length);
                    System.arraycopy(bytes2, 0, bytes3, bytes.length, bytes2.length);
                    return bytes3;
                }else{
                     baos = new ByteArrayOutputStream();
                     oos = new ObjectOutputStream(baos);
                     oos.writeObject(t);
                     byte[] bytes = baos.toByteArray();
                     byte [] bytes2={1};
                     byte [] bytes3 = new byte[bytes.length + bytes2.length];
                     System.arraycopy(bytes, 0, bytes3, 0, bytes.length);
                     System.arraycopy(bytes2, 0, bytes3, bytes.length, bytes2.length);
                     return bytes3;
                }
            } catch (Exception ex) {
                    throw new SerializationException("序列化出错: " + ex.getMessage(), ex);
              }
    }
    @SuppressWarnings("unchecked")
    @Override
    public T deserialize(byte[] bytes) throws SerializationException {
             ByteArrayInputStream bais = null;
            if (bytes == null || bytes.length == 0) {
                return null;
            }
        try {
              if(bytes[bytes.length-1]==0){
                 byte [] b=new byte[bytes.length-1];
                 System.arraycopy(bytes, 0, b, 0, bytes.length-1);
                 T t = (T) Issue.parseFrom(b) ;
                 return t;
            }else if(bytes[bytes.length-1]==1){
                 byte [] b=new byte[bytes.length-1];
                 System.arraycopy(bytes, 0, b, 0, bytes.length-1);
                 bais = new ByteArrayInputStream(b);
                 ObjectInputStream ois = new ObjectInputStream(bais);
                 return (T) ois.readObject();
                }
            return null;
            } catch (Exception ex) {
                ex.printStackTrace();
                throw new SerializationException("Could not deserialize: " + ex.getMessage(), ex);
        }
    }
}

2.redisConfig

**@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
@Override
public Object generate(Object target, java.lang.reflect.Method method, Object... params) {
StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
}      
    @SuppressWarnings("rawtypes")
@Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
        rcm.setDefaultExpiration(60*60);
        return rcm;
    }
    @Bean
    @SuppressWarnings("rawtypes")
    public RedisSerializer robotRedisSerializer() {
        return new RobotRedisSerializer<Object>();
    }
    @Bean
    @SuppressWarnings("rawtypes")
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory, RedisSerializer robotRedisSerializer) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        template.setValueSerializer(robotRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }  
}

猜你喜欢

转载自blog.csdn.net/qq_41255610/article/details/79482155