• @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

  • 相关阅读:
    【Python】科研代码学习:九 parser,argparse,HfArgumentParser
    【论文解读】A Simple Meta-path-free Framework for Heterogeneous Network Embedding
    重新定义音乐创作:ChatGPT与未来音乐产业的融合
    【jquery Ajax 】art-template模板引擎案例——新闻列表
    软考重点10 知识产权
    前端导出Excel并下载到本地
    MindSpore自定义算子:思考,挑战与实践
    JVM性能调优的6大步骤,及关键调优参数详解
    Ubuntu右上角不显示网络的图标解决办法
    八、自定义映射resultMap
  • 原文地址:https://blog.csdn.net/sinat_32502451/article/details/134310654