1. 程式人生 > >redis redistemplate序列化物件配置

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<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, 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;
    }
}