必备redis6教程-分布式缓存Redis6.X+高可用集群课程介绍-小滴课堂

第十一章 SpringCache常用注解介绍+框架案例实战

第1集 SpringCache框架常用之Cacheable实战

简介:SpringCache框架常用注解Cacheable

  • Cacheable注解

    • 标记在一个方法上,也可以标记在一个类上

    • 缓存标注对象的返回结果,标注在方法上缓存该方法的返回值,标注在类上缓存该类所有的方法返回值

    • value 缓存名称,可以有多个

    • key 缓存的key规则,可以用springEL表达式,默认是方法参数组合

    • condition 缓存条件,使用springEL编写,返回true才缓存

  • 案例

//对象@Cacheable(value = {"product"}, key="#root.methodName")//分页@Cacheable(value = {"product_page"},key="#root.methodName + #page+'_'+#size")

  • spEL表达式

    • result

    • root.args[0]

    • root.methodname

    • methodName 当前被调用的方法名

    • args 当前被调用的方法的参数列表

    • result 方法执行后的返回值

第2集 SpringCache框架自定义CacheManager配置和过期时间

简介:SpringCache框架自定义CacheManager配置和过期时间

  • 修改redis缓存序列化器和配置manager过期时间

     

@Bean    @Primary    public RedisCacheManager cacheManager1Hour(RedisConnectionFactory connectionFactory) {        RedisCacheConfiguration config = instanceConfig(3600L);        return RedisCacheManager.builder(connectionFactory)                .cacheDefaults(config)                .transactionAware()                .build();    }    @Bean    public RedisCacheManager cacheManager1Day(RedisConnectionFactory connectionFactory) {        RedisCacheConfiguration config = instanceConfig(3600 * 24L);        return RedisCacheManager.builder(connectionFactory)                .cacheDefaults(config)                .transactionAware()                .build();    }    private RedisCacheConfiguration instanceConfig(Long ttl) {        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);        ObjectMapper objectMapper = new ObjectMapper();        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);        objectMapper.registerModule(new JavaTimeModule());        // 去掉各种@JsonSerialize注解的解析        objectMapper.configure(MapperFeature.USE_ANNOTATIONS, false);        // 只针对非空的值进行序列化        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);        // 将类型序列化到属性json字符串中        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,                ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);        return RedisCacheConfiguration.defaultCacheConfig()                .entryTtl(Duration.ofSeconds(ttl))                .disableCachingNullValues()                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));    }

第3集 SpringCache框架自定义缓存KeyGenerator

简介:SpringCache框架自定义缓存KeyGenerator

  • Key规则定义麻烦,支持自定规则

  • 实战

   

@Bean    public KeyGenerator springCacheDefaultKeyGenerator(){        return new KeyGenerator() {            @Override            public Object generate(Object o, Method method, Object... objects) {                return o.getClass().getSimpleName() + "_"                        + method.getName() + "_"                        + StringUtils.arrayToDelimitedString(objects, "_");            }        };    }

使用

key 属性和keyGenerator属性只能二选一

@Cacheable(value = {"product"},keyGenerator = "springCacheCustomKeyGenerator", cacheManager = "cacheManager1Minute")

第4集 SpringCache框架常用之CachePut实战

简介:SpringCache框架常用注解CachePut

  • CachePut

    • 根据方法的请求参数对其结果进行缓存,每次都会触发真实方法的调用

    • value 缓存名称,可以有多个

    • key 缓存的key规则,可以用springEL表达式,默认是方法参数组合

    • condition 缓存条件,使用springEL编写,返回true才缓存

  • 案例实战

@CachePut(value = {"product"},key = "#productDO.id")

第5集 SpringCache框架常用之CacheEvict实战

简介:SpringCache框架常用注解CacheEvict

  • CacheEvict

    • 代表清除缓存操作是在方法运行之前执行,无论方法是否出现异常,缓存都清除

    • 缓存的清除是否在方法之前执行 ,默认代表缓存清除操作是在方法执行之后执行;

    • 如果出现异常缓存就不会清除

    • 从缓存中移除相应数据, 触发缓存删除的操作

    • value 缓存名称,可以有多个

    • key 缓存的key规则,可以用springEL表达式,默认是方法参数组合

    • beforeInvocation = false

    • beforeInvocation = true

  • 案例实战

@CacheEvict(value = {"product"},key = "#root.args[0]")

第6集 SpringCache框架常用之多注解组合Caching实战

简介:SpringCache框架多注解组合Caching

Caching

组合多个Cache注解使用

允许在同一方法上使用多个嵌套的@Cacheable、@CachePut和@CacheEvict注释

实战

@Caching(            cacheable = {                    @Cacheable(value = "product",keyGenerator = "xdclassKeyGenerator")            },            put = {                    @CachePut(value = "product",key = "#id"),                    @CachePut(value = "product",key = "'stock:'+#id")            }    )

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值