redis redistemplate序列化对象配置

@Configuration
public class RedisConfig {
    /**
     * 重写Redis序列化方式,使用Json方式:
     * 当我们的数据存储到Redis的时候,我们的键(key)和值(value)都是通过Spring提供的Serializer序列化到数据库的。RedisTemplate默认使用的是JdkSerializationRedisSerializer,StringRedisTemplate默认使用的是StringRedisSerializer。
     * Spring Data JPA为我们提供了下面的Serializer:
     * GenericToStringSerializer、Jackson2JsonRedisSerializer、JacksonJsonRedisSerializer、JdkSerializationRedisSerializer、OxmSerializer、StringRedisSerializer。
     * 在此我们将自己配置RedisTemplate并定义Serializer。
     *
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        // 设置值(value)的序列化采用FastJsonRedisSerializer。
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//        redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
        // 设置键(key)的序列化采用StringRedisSerializer。
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

}
 
  



@Component
public class HashRedisTemplate extends RedisTemplate {

    public HashRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        this.setConnectionFactory(redisConnectionFactory);
        this.setKeySerializer(new StringRedisSerializer());
        this.setValueSerializer(new StringRedisSerializer());
        this.setHashKeySerializer(new StringRedisSerializer());
        this.setHashValueSerializer(new StringRedisSerializer());
        this.afterPropertiesSet();
    }
}


下面的方式不推荐,通过java对象序列化的方式,速度上可能不及上面通过json的格式序列化,还有一点就是json序列的方式可以看出对象里面的属性

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

    @Nullable
    @Override
    public byte[] serialize(@Nullable T t) throws SerializationException {
        ByteArrayOutputStream byteArrayOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(t);

            byte[] bytes = byteArrayOutputStream.toByteArray();
            return bytes;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                objectOutputStream.close();
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new byte[0];
    }

    @Nullable
    @Override
    public T deserialize(@Nullable byte[] bytes) throws SerializationException {
        InputStream inputstream = null;
        ObjectInputStream objectInputStream = null;
        try {
            inputstream = new ByteArrayInputStream(bytes);
            objectInputStream = new ObjectInputStream(inputstream);
            Object o = objectInputStream.readObject();
            T t = (T) o;
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                objectInputStream.close();
                inputstream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}




你可能感兴趣的:(redis)