自定义声明式redis缓存序列化fang

package person.txm.springredis.config;


import java.util.HashMap;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;


@Configuration
@EnableCaching
public class CacheConfig {

@Autowired
private RedisConnectionFactory redisConnectionFactory;

@Bean
public RedisTemplate redisTemplate(){
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(redisSerializer());
return redisTemplate;
}

@Bean
public CacheManager cacheManager(){
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate());
cacheManager.setUsePrefix(true);
cacheManager.setDefaultExpiration(86400L);
Map expires = new HashMap();
expires.put("person", 1000L);
cacheManager.setExpires(expires);
return cacheManager;
}

@Bean
public GenericJackson2JsonRedisSerializer redisSerializer() {
ObjectMapper om=new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL,Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
om.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 
om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
return new GenericJackson2JsonRedisSerializer(om);
}
}

你可能感兴趣的:(自定义声明式redis缓存序列化fang)