@EnableCaching @Cacheable @CachePut redis注解缓存

@EnableCaching注解是spring framework中的注解驱动的缓存管理功能。自spring版本3.1起加入了该注解。如果你使用了这个注解,那么你就不需要在XML文件中配置cache manager了。

当你在配置类(@Configuration)上使用@EnableCaching注解时,会触发一个post processor,这会扫描每一个spring bean,查看是否已经存在注解对应的缓存。如果找到了,就会自动创建一个代理拦截方法调用,使用缓存的bean执行处理。

@EnableCaching
@Configuration
public class RedisConfig {

    /**
     * @Cacheable注解不支持配置过期时间,所有需要通过配置CacheManneg来配置默认的过期时间和针对每个类或者是方法进行缓存失效时间配置。
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        return new RedisCacheManager(
                RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
                this.getRedisCacheConfigurationWithTtl(600), // 默认策略,未配置的 key 会使用这个
                this.getRedisCacheConfigurationMap() // 指定 key 策略
        );
    }

    private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
        redisCacheConfigurationMap.put("wxcl", this.getRedisCacheConfigurationWithTtl(60*60*24*7));
        //redisCacheConfigurationMap.put("UserInfoListAnother", this.getRedisCacheConfigurationWithTtl(18000));
        return redisCacheConfigurationMap;
    }

    private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
        StringRedisSerializer keyStringRedisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer<Object> 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);

        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
        redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
                RedisSerializationContext
                        .SerializationPair
                        .fromSerializer(jackson2JsonRedisSerializer)
        ).entryTtl(Duration.ofSeconds(seconds));
        redisCacheConfiguration.serializeKeysWith(
                RedisSerializationContext
                        .SerializationPair
                        .fromSerializer(keyStringRedisSerializer)
        );

        return redisCacheConfiguration;
    }

}
@Slf4j
@Service("myUserCacheService")
public class MyUserCacheService {

    /**
     * sso登录用户获取当前用户
     * @param token token
     * @param user  获取默认null
     */
    @Cacheable(
            value = "iotgw",
            key = "'user'+#token",
            unless = "#result eq null")
    public Map<String, String> getUserFromRedis(String token, Map<String, String> user) {
        log.info("从缓存中获取sso登录人信息失败 {},准备写入缓存", user);
        return user;
    }

    /**
     * 获取企业信息
     * @param token   存储关键值
     * @param company 企业信息
     */
    @Cacheable(
            value = "devicecenter",
            key = "'company'+#token",
            unless = "#result eq null")
    public Map<String, String> getCompanyFromRedis(String token, Map<String, String> company) {
        log.info("从缓存中获取登录人信息失败 {},准备写入缓存", company);
        return company;
    }

    /**
     * 企业信息缓存变更
     * @param token   存储关键值
     * @param company 企业信息
     */
    @CachePut(
            value = "devicecenter",
            key = "'company'+#token",
            unless = "#result eq null")
    public Map<String, String> setCompanyFromRedis(String token, Map<String, String> company) {
        log.info("从缓存中获取登录人信息失败 {},准备写入缓存", company);
        return company;
    }

    /**
     * 后台登录用户获取当前用户
     */
    @Cacheable(
            value = "iotmgw",
            key = "'user'+#userId",
            unless = "#result eq null")
    public Map<String, String> getManagerUserFromRedis(String userId, Map<String, String> user) {
        log.info("从缓存中获取后台登录人信息失败 {},准备写入缓存", user);
        return user;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值