package com.goafanti.core.cache.template;

import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.goafanti.core.cache.serializer.FastJsonRedisSerializer;

public class FastJsonRedisTemplate extends RedisTemplate<String, Object> {

	public FastJsonRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
		setConnectionFactory(redisConnectionFactory);
		
		StringRedisSerializer stringSerializer = new StringRedisSerializer();
		
		RedisSerializer<Object> redisSerializer = new FastJsonRedisSerializer();
		
		this.setKeySerializer(stringSerializer);
		this.setValueSerializer(redisSerializer);
		this.setHashKeySerializer(stringSerializer);
		this.setHashValueSerializer(redisSerializer);
		
		afterPropertiesSet();
	}

	@Override
	protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
		return new DefaultStringRedisConnection(connection);
	}
}
