博客
关于我
spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
阅读量:799 次
发布时间:2023-02-26

本文共 2004 字,大约阅读时间需要 6 分钟。

Spring Cache 实用指南

从 Spring 3.1 开始,Spring 引入了对 Cache 的支持,其使用方法和原理类似于事务管理支持。Spring Cache 作用于方法上,其核心思想是:当调用缓存方法时,会将方法参数和返回结果存储为键值对,下次使用相同参数时直接从缓存中获取结果,避免重复执行。


使用 Spring Cache

使用 Spring Cache 需要完成两方面工作:

  • 声明缓存支持

    使用 @Cacheable@CachePut@CacheEvict 等注解标注方法或类。

  • 配置 Spring Cache 支持

    使用基于注解或 XML 的方式配置 Cache Manager。


  • 基于注解的支持

    @Cacheable

    • 作用:标记支持缓存的方法。

    • 属性

      • value:指定缓存名称。
      • key:自定义生成键。
      • condition:指定缓存条件。
    • 示例

      @Cacheable("users", key="#id")
      public User find(Integer id) {
      return null;
      }
    • 默认键生成策略

      • 无参数:生成 0。
      • 单参数:参数值。
      • 多参数:参数数组的 hashCode。

    @CachePut

    • 作用:强制将方法结果存入缓存。

    • 属性:与 @Cacheable 类似。

    • 示例

      @CachePut("users")
      public User save(User user) {
      return null;
      }

    @CacheEvict

    • 作用:清除缓存元素。

    • 属性

      • value:指定清除的 Cache。
      • key:指定清除的键。
      • condition:清除条件。
      • allEntries:清除所有缓存。
      • beforeInvocation:清除操作时间。
    • 示例

      @CacheEvict(value = "users", allEntries = true)
      public void delete(Integer id) {
      // 清除用户缓存
      }

    @Caching

    • 作用:同时支持多个 Cache 注解。

    • 示例

      @Caching(
      cacheable = @Cacheable("users"),
      evict = { @CacheEvict("cache2"), @CacheEvict(value = "cache3", allEntries = true) }
      )
      public User find(Integer id) {
      return null;
      }

    自定义注解

    • 创建自定义注解并使用它。
    • 示例:
      @Target({ElementType.TYPE, ElementType.METHOD})
      @Retention(RetentionPolicy.RUNTIME)
      @Cacheable(value = "users")
      public @interface MyCacheable {
      }

    配置 Spring Cache

    基于注解

    • 在 Spring 配置文件中引入 cache 命名空间,启用注解驱动。

    基于 XML

    • 使用 cache:advice 定义 Cache 处理逻辑。

    配置 Cache Manager

    ConcurrentMap

    • 示例:

    Ehcache

    • 示例:

    键的生成策略

    • 默认策略

      • 无参数:0。
      • 单参数:参数值。
      • 多参数:参数数组的 hashCode。
    • 自定义策略

      • 使用 Spring EL 表达式生成键。
      • 示例:
        @Cacheable(value = "users", key = "#user.id")
        public User find(User user) {
        return null;
        }

    使用 Ehcache

    • 单独使用 Ehcache:
      • 配置 EhCacheManagerFactoryBean 和 EhCacheFactoryBean。
      • 示例:

    总结

    Spring Cache 提供了灵活的缓存支持,通过注解和 XML 配置方便快速实现。理解其原理和使用方法,对于高效应用开发至关重要。

    转载地址:http://tdvfk.baihongyu.com/

    你可能感兴趣的文章
    Oracle Spatial空间数据库建立
    查看>>
    UML— 活动图
    查看>>
    oracle sqlplus已停止工作,安装完成客户端后sqlplus报“段错误”
    查看>>
    Oracle Statspack分析报告详解(一)
    查看>>
    oracle tirger_在Oracle中,临时表和全局临时表有什么区别?
    查看>>
    oracle where 条件的执行顺序分析1
    查看>>
    Oracle 中的 decode
    查看>>
    oracle 使用leading, use_nl, rownum调优
    查看>>
    oracle 修改字段类型方法
    查看>>
    oracle 内存参数示意图
    查看>>
    Oracle 写存储过程的一个模板还有一些基本的知识点
    查看>>
    Oracle 创建 DBLink 的方法
    查看>>
    oracle 创建双向备份,Materialized View 物化视图实现 Oracle 表双向同步
    查看>>
    oracle 创建字段自增长——两种实现方式汇总
    查看>>
    Oracle 升级10.2.0.5.4 OPatch 报错Patch 12419392 Optional component(s) missing 解决方法
    查看>>
    oracle 可传输的表空间:rman
    查看>>
    Oracle 启动监听命令
    查看>>
    Oracle 在Drop表时的Cascade Constraints
    查看>>
    Oracle 在Sqlplus 执行sql脚本文件。
    查看>>
    Oracle 如何处理CLOB字段
    查看>>