Spring缓存注解@Cacheable详细介绍和实际使用案例

@Cacheable是Spring框架中用于缓存方法返回结果的注解,它可以显著提高应用程序的性能,特别是对于一些计算密集型或频繁调用且结果不经常变化的方法。以下是关于@Cacheable的详细介绍:

基本使用

  • 添加依赖:使用@Cacheable注解前,需要在项目的pom.xml中添加Spring Cache依赖,例如使用Spring Boot时,可以添加以下依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency>
  • 启用缓存:在Spring Boot的主应用程序类上添加@EnableCaching注解来启用缓存功能,如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}
}
  • 使用@Cacheable注解方法:在需要缓存结果的方法上添加@Cacheable注解,如下:
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class MyService {@Cacheable("myCache")public String getCachedData(String key) {// 模拟耗时操作try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return "Data for key: " + key;}
}

在上述示例中,@Cacheable("myCache")注解告诉Spring将该方法的结果存储在名为myCache的缓存中。当这个方法被调用时,Spring首先检查myCache中是否已经有了对应key的缓存结果。如果有,直接从缓存中返回结果,而不执行方法体;如果没有,则执行方法并将结果存储在缓存中。

注解属性

以下是对 @Cacheable 注解的主要参数的详细介绍:

1. value

  • 作用
    • 指定缓存的名称。它是 @Cacheable 注解最常用的参数之一,用来标识一个或多个缓存存储区域。当方法的返回结果需要存储时,会被存储在这些指定的缓存名称下。cacheNames参数与之相同。
  • 使用示例
    @Cacheable(value = "userCache")
    public User getUserById(Long id) {// 业务逻辑
    }
    
    在上述示例中,方法 getUserById 的结果将被存储在名为 userCache 的缓存区域中。
  • 多值使用
    • 可以使用一个数组来指定多个缓存名称,此时结果会被存储在多个缓存区域中。
    @Cacheable(value = {"cache1", "cache2"})
    public String getSomeData(String key) {// 业务逻辑
    }
    
    当调用 getSomeData 方法时,其结果会同时存储在 cache1cache2 缓存区域中。

2. key

  • 作用
    • 用于指定缓存存储和检索的键。默认情况下,会使用方法的参数作为键,但可以使用 SpEL(Spring Expression Language)自定义键的生成表达式。
  • 使用示例(使用参数作为键)
    @Cacheable(value = "userCache", key = "#id")
    public User getUserById(Long id) {// 业务逻辑
    }
    
    这里使用 #id 作为键,即调用 getUserById 方法时传入的 id 参数将作为缓存的键。
  • 使用示例(使用多个参数作为键)
    @Cacheable(value = "userCache", key = "#firstName + '-' + #lastName")
    public User getUserByName(String firstName, String lastName) {// 业务逻辑
    }
    
    此示例中,将使用 firstName lastName 参数组合(使用 - 分隔)作为缓存的键。
  • 使用示例(使用方法信息作为键)
    @Cacheable(value = "userCache", key = "#root.methodName + '_' + #id")
    public User getUserById(Long id) {// 业务逻辑
    }
    
    这里使用方法名称和 id 参数组合作为键,例如对于 getUserById(1L) 的调用,键可能是 getUserById_1
  • 特殊键生成器使用
    • 可以使用自定义的键生成器,需要实现 org.springframework.cache.interceptor.KeyGenerator 接口,并在 @Cacheable 注解中引用。
    @Cacheable(value = "userCache", keyGenerator = "myCustomKeyGenerator")
    public User getUserById(Long id) {// 业务逻辑
    }
    

3. condition

  • 作用
    • 用于指定一个 SpEL 表达式,当表达式的结果为 true 时,才会进行缓存操作。可以根据方法的参数、返回值或其他信息来决定是否缓存结果。
  • 使用示例
    @Cacheable(value = "userCache", condition = "#id > 0")
    public User getUserById(Long id) {// 业务逻辑
    }
    
    仅当 id 参数大于 0 时,才会将 getUserById 方法的结果存储到 userCache 中。
  • 更复杂的条件示例
    @Cacheable(value = "userCache", condition = "#user.age > 18 && #user.active")
    public User getUser(User user) {// 业务逻辑
    }
    
    仅当 user 的年龄大于 18 岁且 active 属性为 true 时,结果才会被缓存。

4. unless

  • 作用
    • 也是使用 SpEL 表达式,当表达式的结果为 true 时,不会将结果存储在缓存中。与 condition 相反,condition 决定是否缓存,unless 决定不缓存的情况。
  • 使用示例
    @Cacheable(value = "userCache", unless = "#result == null")
    public User getUserById(Long id) {// 业务逻辑
    }
    
    如果 getUserById 方法的返回结果为 null,则不会将结果存储在 userCache 中。
  • 使用示例(根据结果属性判断)
    @Cacheable(value = "userCache", unless = "#result.name == null")
    public User getUserById(Long id) {// 业务逻辑
    }
    
    getUserById 的返回结果的 name 属性为 null 时,不会存储该结果。

5. cacheManager

  • 作用
    • 可以指定一个特定的缓存管理器来管理缓存操作,用于在多个缓存管理器存在的情况下,选择使用哪个缓存管理器。
  • 使用示例
    @Cacheable(value = "userCache", cacheManager = "customCacheManager")
    public User getUserById(Long id) {// 业务逻辑
    }
    
    上述代码中,使用名为 customCacheManager 的缓存管理器来管理 userCache 中的缓存操作。

6. cacheResolver

  • 作用
    • 可以指定一个特定的缓存解析器,用于更灵活地解析缓存操作的存储位置,实现 org.springframework.cache.interceptor.CacheResolver 接口的自定义缓存解析器。
  • 使用示例
    @Cacheable(value = "userCache", cacheResolver = "myCacheResolver")
    public User getUserById(Long id) {// 业务逻辑
    }
    

7. sync

  • 作用
    • 是一个布尔值参数,用于指示是否以同步方式执行缓存操作。默认为 false,表示使用异步方式。
    • 当设置为 true 时,同一时间内只有一个线程可以执行该方法,其他线程会阻塞等待缓存结果,避免多个线程同时执行该方法。
  • 使用示例
    @Cacheable(value = "userCache", sync = true)
    public User getUserById(Long id) {// 业务逻辑
    }
    

这些参数可以灵活组合使用,以满足不同的缓存需求,通过使用 @Cacheable 及其参数,可以在 Spring 应用程序中轻松实现缓存机制,提高系统性能和响应速度,同时也需要根据具体的业务场景和性能需求,合理调整这些参数的使用。

缓存工作原理

  • 当一个被@Cacheable注解的方法被调用时,Spring的AOP机制会拦截该方法的调用。
  • 首先,根据valuekey属性查找缓存中是否已经存储了相应的结果。
  • 如果缓存中存在结果,则直接返回缓存中的结果,不执行方法体。
  • 如果缓存中不存在结果,则执行方法体,将结果存储在缓存中,并返回结果。

适用场景

  • 数据访问层:在数据访问层中,对于经常查询但很少修改的数据,如配置信息、字典数据等,可以使用@Cacheable进行缓存,减少对数据库的访问。
  • 业务逻辑层:对于一些复杂的计算逻辑,如计算用户积分、统计报表等,当输入参数相同时,可以使用@Cacheable避免重复计算。

注意事项

  • 缓存更新:当被缓存的数据发生变化时,需要使用@CacheEvict@CachePut等注解来更新或清除缓存,以保证数据的一致性。
  • 缓存失效:缓存的存储是有限的,需要考虑缓存失效的策略,如LRU(Least Recently Used)等,避免内存溢出。
  • 序列化:存储在缓存中的对象需要是可序列化的,因为缓存存储可能会将对象存储在分布式环境中,如Redis等,确保对象实现了Serializable接口或使用其他序列化机制。

与其他缓存注解的配合使用

  • @CacheEvict:用于清除缓存,例如:
    @CacheEvict(value = "myCache", key = "#key")
    public void clearCache(String key) {// 清除myCache中指定key的缓存
    }
    
  • @CachePut:用于更新缓存,无论缓存中是否存在结果,都会执行方法体,并将结果存储在缓存中,例如:
    @CachePut(value = "myCache", key = "#key")
    public String updateCachedData(String key) {return "Updated data for key: " + key;
    }
    

缓存存储的选择

  • 可以使用Spring默认的缓存存储,也可以集成外部缓存,如Redis、Ehcache等。集成外部缓存时,需要在配置文件中添加相应的配置,例如使用Redis时:
spring:cache:type: redisredis:cache-null-values: false

通过合理使用@Cacheable和其他缓存注解,可以有效提高应用程序的性能和响应速度,同时需要根据具体的应用场景和需求,灵活运用这些注解,确保缓存的一致性和有效性。

实际使用案例

以下是一个更完整的 @Cacheable 注解的实际使用案例,结合了一个简单的 Spring Boot 应用程序,包括服务层、控制器和数据访问层,展示如何在不同场景下使用 @Cacheable 注解来提高性能:

1. 添加依赖

首先,在 pom.xml 中添加 Spring Boot 的缓存依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency>

2. 启用缓存

在 Spring Boot 的主应用程序类上添加 @EnableCaching 注解,以启用缓存功能:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class CacheableApplication {public static void main(String[] args) {SpringApplication.run(CacheableApplication.class, args);}
}

3. 数据访问层 (Repository)

假设我们有一个简单的数据访问接口和实现类,模拟从数据库中获取用户信息:

import org.springframework.stereotype.Repository;@Repository
public interface UserRepository {User findById(Long id);
}import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.Map;@Repository
public class UserRepositoryImpl implements UserRepository {private final Map<Long, User> userMap = new HashMap<>();public UserRepositoryImpl() {// 初始化一些用户数据userMap.put(1L, new User(1L, "Alice"));userMap.put(2L, new User(2L, "Bob"));userMap.put(3L, new User(3L, "Charlie"));}@Overridepublic User findById(Long id) {// 模拟从数据库获取数据的延迟try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return userMap.get(id);}
}

4. 服务层 (Service)

在服务层使用 @Cacheable 注解来缓存 findById 方法的结果:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class UserService {@Autowiredprivate UserRepository userRepository;@Cacheable(value = "userCache", key = "#id")public User findUserById(Long id) {return userRepository.findById(id);}
}

5. 控制器 (Controller)

在控制器中调用服务层的方法,观察缓存的效果:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/users/{id}")public User getUserById(@PathVariable Long id) {return userService.findUserById(id);}
}

6. 实体类 (Entity)

定义一个简单的 User 实体类:

import java.io.Serializable;public class User implements Serializable {private Long id;private String name;public User() {}public User(Long id, String name) {this.id = id;this.name = name;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

7. 测试缓存效果

  • 当你第一次访问 http://localhost:8080/users/1 时,会看到一个大约 2 秒的延迟,因为它会调用 UserRepositoryImplfindById 方法从模拟的数据库中获取数据。
  • 再次访问 http://localhost:8080/users/1 时,会立即返回结果,因为结果已经被缓存,不会再调用 findById 方法。

8. 高级使用

  • 使用条件缓存
    @Cacheable(value = "userCache", key = "#id", condition = "#id > 0")
    public User findUserById(Long id) {return userRepository.findById(id);
    }
    
    上述代码中,仅当 id 大于 0 时才会进行缓存。
  • 使用 unless 排除缓存结果
    @Cacheable(value = "userCache", key = "#id", unless = "#result == null")
    public User findUserById(Long id) {return userRepository.findById(id);
    }
    
    当结果为 null 时,不缓存结果。

9. 结合其他缓存注解

  • 可以使用 @CachePut 来更新缓存:
    import org.springframework.cache.annotation.CachePut;
    import org.springframework.stereotype.Service;@Service
    public class UserService {@Autowiredprivate UserRepository userRepository;@Cacheable(value = "userCache", key = "#id")public User findUserById(Long id) {return userRepository.findById(id);}@CachePut(value = "userCache", key = "#user.id")public User updateUser(User user) {// 模拟更新用户信息return userRepository.update(user);}
    }
    
    当调用 updateUser 方法时,会更新缓存中的用户信息。
  • 使用 @CacheEvict 来清除缓存:
    import org.springframework.cache.annotation.CacheEvict;
    import org.springframework.stereotype.Service;@Service
    public class UserService {@Autowiredprivate UserRepository userRepository;@Cacheable(value = "userCache", key = "#id")public User findUserById(Long id) {return userRepository.findById(id);}@CacheEvict(value = "userCache", key = "#id")public void deleteUser(Long id) {userRepository.delete(id);}
    }
    
    当调用 deleteUser 方法时,会清除 userCache 中对应 id 的缓存。

10. 缓存管理

  • 可以使用不同的缓存管理器或缓存解析器,例如使用 Redis 作为缓存存储:
    spring:cache:type: redisredis:cache-null-values: false
    
    并在 @Cacheable 中指定:
    @Cacheable(value = "userCache", cacheManager = "redisCacheManager")
    public User findUserById(Long id) {return userRepository.findById(id);
    }
    

通过以上示例,你可以看到如何在一个实际的 Spring Boot 应用程序中使用 @Cacheable 注解来实现缓存功能,提高应用程序的性能和响应速度,同时根据不同的业务场景和需求,灵活运用 @Cacheable 及其相关注解。你可以通过测试不同的请求和观察日志,来更好地理解缓存的存储、更新和清除操作。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/67813.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第10章:Python TDD优化货币类方法与引入工厂方法

写在前面 这本书是我们老板推荐过的&#xff0c;我在《价值心法》的推荐书单里也看到了它。用了一段时间 Cursor 软件后&#xff0c;我突然思考&#xff0c;对于测试开发工程师来说&#xff0c;什么才更有价值呢&#xff1f;如何让 AI 工具更好地辅助自己写代码&#xff0c;或许…

解决 Django 5.1 中的 TemplateSyntaxError 错误

解决 Django 5.1 中的 TemplateSyntaxError 错误 在 Django 开发过程中&#xff0c;我们经常会遇到 TemplateSyntaxError 错误&#xff0c;尤其是在模板文件中使用不被支持或错误的模板标签时。最近&#xff0c;我们遇到的一个常见错误是&#xff1a; Invalid block tag on l…

多模态数据有哪些常见类型?从视觉到语言的广泛应用

1. 图像文字&#xff1a;让机器“看懂”世界 这是什么&#xff1f; 机器看一张图片&#xff0c;然后用文字描述出来&#xff0c;比如“这是一只正在奔跑的狗”。 它有什么用&#xff1f; 购物&#xff1a;拍张鞋子的照片&#xff0c;AI能告诉你哪家网店有同款。 辅助盲人&…

【Oracle数据库】创建表的同义词示例

目录 背景创建用户创建基础表及数据创建同义词回收权限删除用户 背景 测试Oracle数据库创建同义词功能。 创建用户 -- 切换到 SYS 用户 CONNECT SYS/your_password AS SYSDBA;-- 创建 HR 用户 CREATE USER HR IDENTIFIED BY hr_password; GRANT DBA TO HR;-- 创建 SCOTT 用户…

【数据库知识】PostgreSQL介绍

PostgreSQL介绍 概述一、起源与发展二、特性与功能三、PL/pgSQL语言四、应用场景五、配置与优化 核心概念一、基础数据结构二、数据操作三、高级特性四、应用场景 多版本控制MVCC说明一、MVCC的基本概念二、MVCC的实现原理三、MVCC的工作流程四、MVCC的优势五、MVCC的局限性 可…

gitlab使用多数据库

1. 说明 默认情况下&#xff0c;GitLab 使用一个单一的应用数据库&#xff0c;称为主数据库。为了扩展 GitLab&#xff0c;您可以将 GitLab 配置为使用多个应用数据库。 设置多个数据库后&#xff0c;GitLab 将使用第二个应用数据库用于 CI/CD 功能&#xff0c;称为 CI 数据库…

常用排序算法之插入排序

目录 前言 一、基本原理 1.算法步骤 2.动画演示 3.插入排序的实现代码 二、插入排序的时间复杂度 1. 时间复杂度 1.最优时间复杂度 2.最差时间复杂度 3.平均时间复杂度 2. 空间复杂度 三、插入排序的优缺点 1.优点 2.缺点 四、插入排序的改进与变种 五、插入排…

【机器学习实战入门】使用OpenCV进行性别和年龄检测

Gender and Age Detection Python 项目 首先,向您介绍用于此高级 Python 项目的性别和年龄检测中的术语: 什么是计算机视觉? 计算机视觉是一门让计算机能够像人类一样观察和识别数字图像和视频的学科。它面临的挑战大多源于对生物视觉有限的了解。计算机视觉涉及获取、处…

python爬虫的学习流程(1-前提准备)

这里主要记录一下我的python爬虫初级的学习的流程 1.python爬虫入门实战课 什么是爬虫&#xff1f;众说纷纭&#xff0c;我们引用维基百科上对网络爬虫的介绍&#xff1a; 网络爬虫&#xff08;英语&#xff1a;Web crawler&#xff09;&#xff0c;也叫网络蜘蛛&#xff08;…

语音技术在播客领域的应用(2)

播客是以语音为主&#xff0c;各种基于AI 的语音技术在播客领域十分重要。 语音转文本 Whisper Whisper 是OpenAI 推出的开源语音辨识工具&#xff0c;可以把音档转成文字&#xff0c;支援超过50 种语言。这款工具是基于68 万小时的训练资料&#xff0c;其中包含11.7 万小时的…

PyTorch使用教程(13)-一文搞定模型的可视化和训练过程监控

一、简介 在现代深度学习的研究和开发中&#xff0c;模型的可视化和监控是不可或缺的一部分。PyTorch&#xff0c;作为一个流行的深度学习框架&#xff0c;通过其丰富的生态系统提供了多种工具来满足这一需求。其中&#xff0c;torch.utils.tensorboard 是一个强大的接口&…

C#语言的学习路线

C#语言的学习路线 C#作为一种现代编程语言&#xff0c;凭借其简洁的语法、强大的功能和广泛的应用&#xff0c;得到了越来越多开发者的青睐。无论是开发桌面应用、Web应用、游戏&#xff0c;还是云服务&#xff0c;C#都有着广泛的应用场景。本文将为有志于学习C#的读者提供一条…

学习ASP.NET Core的身份认证(基于JwtBearer的身份认证6)

重新创建WebApi项目&#xff0c;安装Microsoft.AspNetCore.Authentication.JwtBearer包&#xff0c;将之前JwtBearer测试项目中的初始化函数&#xff0c;jwt配置类、token生成类全部挪到项目中。   重新编写login函数&#xff0c;之前测试Cookie和Session认证时用的函数适合m…

第8章:Python TDD处理货币类代码重复问题

写在前面 这本书是我们老板推荐过的&#xff0c;我在《价值心法》的推荐书单里也看到了它。用了一段时间 Cursor 软件后&#xff0c;我突然思考&#xff0c;对于测试开发工程师来说&#xff0c;什么才更有价值呢&#xff1f;如何让 AI 工具更好地辅助自己写代码&#xff0c;或许…

Java 设计模式 二 单例模式 (Singleton Pattern)

单例模式 (Singleton Pattern) 是一种常见的设计模式&#xff0c;属于创建型模式。它的核心思想是确保一个类只有一个实例&#xff0c;并提供一个全局访问点来获取该实例。通常用于那些需要全局控制的场景&#xff0c;比如配置管理、日志系统、数据库连接池等。 1. 单例模式的…

【Linux系统】—— 编译器 gcc/g++ 的使用

【Linux系统】—— 编译器 gcc/g 的使用 1 用 gcc 直接编译2 翻译环境2.1 预处理&#xff08;进行宏替换&#xff09;2.2 编译&#xff08;生成汇编&#xff09;2.3 汇编&#xff08;生成机器可识别代码&#xff09;2.4 链接2.5 记忆小技巧2.6 编译方式2.7 几个问题2.7.1 如何理…

[已解决]chatgpt被降智了怎么办?(无法联网、识别图片、文件、画图)

文章目录 1、治标办法一发图2、治本方法—使用ChatGPT中国区代理官方站点 1、治标办法一发图 该方法原本就有&#xff0c;但是在1.1日ChatGPT降智事件中突然失效。于1月11日&#xff0c;该方法又突然有效&#xff0c;因此也标志着本次ChatGPT降智事件的结束。当你ChatGPT出现降…

缓存、数据库双写一致性解决方案

双写一致性问题的核心是确保数据库和缓存之间的数据同步&#xff0c;以避免缓存与数据库数据不同步的问题&#xff0c;尤其是在高并发和异步环境下。本文将探讨双写一致性面临的主要问题和解决方案&#xff0c;重点关注最终一致性。 本文讨论的是最终一致性问题 双写一致性面…

element el-table合并单元格

合并 表格el-table添加方法:span-method"” <el-table v-loading"listLoading" :data"SHlist" ref"tableList" element-loading-text"Loading" border fit highlight-current-row :header-cell-style"headClass" …

【学习总结|DAY034】Maven高级

在 Web 后端开发中&#xff0c;Maven 作为强大的项目管理工具&#xff0c;其高级特性对于优化项目结构、提高开发效率至关重要。本文将结合实际代码示例&#xff0c;深入介绍 Maven 的分模块设计与开发、继承与聚合以及私服相关知识。 一、分模块设计与开发 &#xff08;一&a…