• @Cacheable 、 @CachePut 、@CacheEvict 注解


    使用场景

    在开发中,需要使用缓存时,可以使用 @Cacheable 、 @CachePut 、@CacheEvict 注解。

    在 Application 类上添加注解 @EnableCaching

    @EnableCaching
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    @Cacheable 注解

    能够让方法的返回值被缓存起来,后续的请求可以直接从缓存中获取结果。

    示例:

        @Cacheable( cacheManager = "cacheManagerTwoHour",
                value = "cache:id:test",
                key = "#id",
                condition = "#id!=null")
        public String getList(String id) {
        	//数据表查询
            return configService.getNameById(id);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    解释如下

    cacheManager :可以用来指定缓存管理器。从哪个缓存管理器里面获取缓存。非必需。
    value 是缓存key的前缀。
    key 是缓存的key,其中的 #后面可以带上对象/参数。
    condition 是条件。只有符合条件,缓存注解才会生效。
    
    • 1
    • 2
    • 3
    • 4

    SpringCache注解,会自动拼接好缓存的key,并在中间加上符号:: ,

    比如 value = "cache:id:test",  key = "#id",  当参数id为12345时,那么真实的缓存 key 是
    cache:id:test::12345
    
    • 1
    • 2

    如果变更时使用 StringRedisTemplate 处理缓存,记得要把这个符号 :: 拼到缓存key里面。

    方法参数为对象

    示例:

    如果 key 由多个字段组成,可以通过 # 指定对象字段,再拼接起来。
    如果是所有的字段,可以用 #queryDto.toString().

    @Cacheable(cacheManager = "cacheManagerTwoHour",
            value = "cache:name:test:",
            key = "#queryDto.userId + ':' + #queryDto.channel",
            condition = "#queryDto.queryType == null or #queryDto.queryType==0")
       public String getListByDto(QueryDto queryDto) {
        	//数据表查询
            return configService.getNameById(id);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    如何测试@Cacheable注解是否生效?

    先查询一次该方法,然后修改数据库数据,再查询一次方法。

    如果 @Cacheable 生效,那查出来的就是缓存的数据,而不是数据库的数据。

    @CachePut

    对key进行缓存,缓存的值为方法的返回值。可以在数据更新时使用。方法仍然会执行。

    以下方法执行后,缓存的key为参数id,缓存对应的值为 entity。

        @CachePut(
                value = "cache:id:test",
                key = "#id",
                condition = "#id!=null")
        public ConfigEntity update(String id) {
            //数据表查询
            ConfigEntity entity = new ConfigEntity();
            entity.setWxBrandId("brandTest456");
            entity.setId("12345");
    
            configService.updateById(entity);
            return entity;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    @Cacheable和@CachePut 的区别:

    @Cacheable: 当重复使用相同参数调用方法的时候,不会再次执行方法 ,

    方法的结果直接从缓存中找到并返回了。

    @CachePut: 方法一直会被执行,同时方法的返回值也被记录到缓存中。

    @CacheEvict

    删除缓存中指定key的数据的。

        @CacheEvict(
                value = "cache:id:test",
                key = "#id",
                condition = "#id!=null")
    	public void update(String id) {
            configService.updateById(id);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    执行方法后,缓存中的 key及参数 就被删除了。

    参考资料:

    https://blog.csdn.net/zl1zl2zl3/article/details/110987968

  • 相关阅读:
    css实现鼠标悬停时元素的显示与隐藏
    【图像分割】实战篇(1)传统图像分割
    【Leetcode Sheet】Weekly Practice 11
    AI插件开发-illustrator API介绍-插件组件架构-常量-文本引擎-illustrator插件-对话管理器-Caller-Selector
    Free MyBatis plugin搜索不到解决,最新2021.12.09版本下载
    R语言利用openxlsx包输出EXCEL报告
    智能手表上的音频(二):驱动
    PDF格式分析(七十四)——自由文本注释(Free Text)
    色氨酸乙酯双三氟甲基磺酰亚胺[TrpC2][Tf2N]离子液体
    【salesforce平台基础】-想到啥写点啥
  • 原文地址:https://blog.csdn.net/sinat_32502451/article/details/134310654