package com.goafanti.core.shiro.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.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

public class SessionRedisTemplate extends RedisTemplate<String, Object> {

	public SessionRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
		setConnectionFactory(redisConnectionFactory);
		StringRedisSerializer stringSerializer = new StringRedisSerializer();
		RedisSerializer<Object> redisSerializer = new JdkSerializationRedisSerializer(this.getClass().getClassLoader());
		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);
	}
}
