SpringBoot2集成RedisCacheManager

2019-05-17  本文已影响0人  南岩飞雪

maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.0.3.RELEASE</version>
</dependency>

RedisConfig

@Configuration
@ConfigurationProperties(prefix = "redis")
@EnableCaching  // 需要这个注解才能启用注解驱动的缓存管理功能
public class RedisConfig {
    private String clusterNodes;
    private String redisHost;
    private int redisPort;
    private String redisPasswd;
    private int timeOut = 2000;
    private int redirects = 8;

    @Bean // redis配置
    public RedisClusterConfiguration getClusterConfiguration() {
        Map<String, Object> source = Maps.newHashMap();
        source.put("spring.redis.cluster.nodes", clusterNodes);
        source.put("spring.redis.cluster.timeout", timeOut);
        source.put("spring.redis.cluster.max-redirects", redirects);
        return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
    }

    @Bean // redis连接
    public RedisConnectionFactory redisConnectionFactory() {
        JedisConnectionFactory cf = null;
        if (clusterNodes != null && !clusterNodes.isEmpty()) {
            cf = new JedisConnectionFactory(getClusterConfiguration());
        } else {
            RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
            redisStandaloneConfiguration.setHostName(redisHost);
            redisStandaloneConfiguration.setPort(redisPort);
            redisStandaloneConfiguration.setPassword(RedisPassword.of(redisPasswd));
            cf = new JedisConnectionFactory(redisStandaloneConfiguration);
        }
        cf.afterPropertiesSet();
        return cf;
    }

    @Bean // 实际使用的redisTemplate,可以直接注入到代码中,直接操作redis
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        return redisTemplate;
    }

    @Bean // 关联redis到注解
    CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {

        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);

        // 默认配置,过期时间指定是30分钟
        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
        defaultCacheConfig.entryTtl(Duration.ofMinutes(30));

        // redisExpire1h cache配置,过期时间指定是1小时,缓存key的前缀指定成prefixaaa_(存到redis的key会自动添加这个前缀)
        RedisCacheConfiguration userCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().
                entryTtl(Duration.ofHours(1)).prefixKeysWith("prefixaaa_");
        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
        redisCacheConfigurationMap.put("redisExpire1h", userCacheConfiguration);

        RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig, redisCacheConfigurationMap);
        return cacheManager;
    }

使用注解驱动的缓存管理功能

@Repository("testUserDAO")
public interface TestUserDAO {
    // 查询操作---缓存
    @Cacheable(value = "redisExpire1h", key = "'test_user_'.concat(#p0)")
    TestUser selectById(@Param("id") String id);

    // 更新操作---清除缓存
    @CacheEvict(value = "redisExpire1h", key = "'test_user_'.concat(#p0.id)")
    Integer updateConfigByCorp(TestUser testUser);
}

null值处理

参考

https://docs.spring.io/spring/docs/3.1.0.M1/spring-framework-reference/html/cache.html#cache-spel-context

为什么阿里巴巴要求程序员谨慎修改serialVersionUID 字段的值

扩展

Mysql缓存机制,Mybatis缓存机制

上一篇下一篇

猜你喜欢

热点阅读