SpringBoot +Redis 自定义模板

Redis序列化对象
GenericToStringSerializer: 可以将任何对象泛化为字符串并序列化。
JacksonJsonRedisSerializer: 序列化object对象为json字符串。
Jackson2JsonRedisSerializer: 跟JacksonJsonRedisSerializer实际上是一样的。
JdkSerializationRedisSerializer: 序列化java对象,对象必须实现Serializable接口。
StringRedisSerializer: 简单的字符串序列化。

Redis配置:

@Configuration
@EnableCaching
public class RedisConfigurer extends CachingConfigurerSupport {

    /*定义缓存数据 key 生成策略的bean
    包名+类名+方法名+所有参数
    */
    @Bean
    public KeyGenerator wiselyKeyGenerator(){
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };

    }

    @Bean
    public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
        // 创建缓存管理器
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        cacheManager.setDefaultExpiration(3600 * 24);
        cacheManager.setUsePrefix(true);
        cacheManager.setCachePrefix(new DefaultRedisCachePrefix(":"));
        return cacheManager;
    }

    @Bean
    public RedisTemplate redisTemplate(JedisConnectionFactory factory) {
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public RedisTemplate sessionRedisTemplate(JedisConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(factory);
        template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new JdkSerializationRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}

使用模板

@Resource
private StringRedisTemplate stringRedisTemplate;
......
@Resource(name = "redisTemplate")
private HashOperations hashOperations;
......
@Resource(name = "sessionRedisTemplate")
private ValueOperations valueOperations;

Spring 缓存注解*@Cacheable的使用

@Cacheable(value = "domainHome", keyGenerator = "wiselyKeyGenerator", unless = "#result.empty")
public List getDomains(String serverName) {
    ......
}

你可能感兴趣的:(SpringBoot +Redis 自定义模板)