• @ENABLECACHING 基于注解的缓存


    @EnableCaching
    • @Cacheable
    指定一个或多个Cache名字,同属性cacheNames
    Spring Cache 使用 ---@EnableCaching @Cacheable 注解

    @Cacheable(value ="sampleCache")
    @Cacheable(cacheNames="sampleCache")


    • @CacheEvict
    用于仅清除缓存
    例子里的注解 @CacheEvict 中存在有以下几个元素 
    - value (也可使用 cacheNames) : 同Cacheable注解,可看做命名空间。表示删除哪个命名空间中的缓存 
    - allEntries: 标记是否删除命名空间下所有缓存,默认为false 
    - key: 同Cacheable注解,代表需要删除的命名空间下唯一的缓存key。
    第一段,与 @Scheduled 注解同时使用,每十秒删除命名空间name下所有的缓存。
    第二段,调用此方法后删除命名空间models下, key == 参数 的缓存 同样含有unless与condition

    复制代码
    @CacheEvict(value = "models", allEntries = true)
    @Scheduled(fixedDelay = 10000)
    public void deleteFromRedis() {
    }
    
    @CacheEvict(value = "models", key = "#name")
    public void deleteFromRedis(String name) {
    }
    复制代码

    • @CachePut
    用于仅存放缓存
    例子里的注解 @CachePut 中存在有以下几个元素
    value: 同上
    key: 同上
    condition(unless): 同上
    比如可用于后台保存配置时及时刷新缓存。
    SpringCache缓存初探

    @CachePut(value = "models", key = "#name")
    public TestModel saveModel(String name, String address) {
    return new TestModel(name, address);
    }


    • @Caching
    用于在一个方法或者类上同时指定多个Spring Cache相关的注解

    @Caching(cacheable = {@Cacheable(value = "userCache", key = "#a0", unless = "#")
    ,@Cacheable(value = "userCache", key = "#a0", unless = "#")})


    • @CacheConfig
    这个注解是用于在同一个类中共享一些基础的cache配置的
    一个类级别的注解,允许共享缓存的名称、KeyGenerator、CacheManager 和CacheResolver。 
    该操作会被覆盖。

    //开启缓存注解

    @Configuration
    @EnableCaching
    public class AppConfig {
    }
  • 相关阅读:
    IDEA 远程调试springboot
    Mybatitas-plus实现逻辑删除
    java通过poi导出excel
    js 展示当前时间
    Linux环境下服务自启
    Spring自带定时器@Scheduled
    Quartz任务调度框架相关方法、参数理解
    Quartz 实现定时任务
    mysql 使用union(all) + order by 导致排序失效
    mongodb的查询操作
  • 原文地址:https://www.cnblogs.com/it-deepinmind/p/11764429.html
Copyright © 2020-2023  润新知