Skip to content
Back to skills

Spring Boot Caffeine 缓存配置与注解使用

ASecurity

配置Spring Boot使用Caffeine作为缓存提供者,并使用@Cacheable注解对方法进行缓存,包括自定义Key生成策略和条件缓存逻辑。

  • 2 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 27, 2026
ai-agentsjavaspring

Security analysis

A100/100

Scanned September 27, 2026

npx -y skills add David-Li0406/meta-skill-evloving --skill 'spring-boot-caffeine-缓存配置与注解使用' --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Spring Boot Caffeine 缓存配置与注解使用?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Spring Boot Caffeine 缓存配置与注解使用
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/david-li0406-spring-boot-caffeine/badge)](https://www.skillsdirectory.com/skills/david-li0406-spring-boot-caffeine)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "d791affa-b682-4f23-b89f-7225fc0d3246"
name: "Spring Boot Caffeine 缓存配置与注解使用"
description: "配置Spring Boot使用Caffeine作为缓存提供者,并使用@Cacheable注解对方法进行缓存,包括自定义Key生成策略和条件缓存逻辑。"
version: "0.1.0"
tags:
  - "spring"
  - "caffeine"
  - "cache"
  - "java"
  - "annotation"
triggers:
  - "spring 配置 caffeine 缓存"
  - "怎么使用 @Cacheable 注解"
  - "spring 缓存 key 拼接"
  - "caffeine 缓存 unless 条件"
---

# Spring Boot Caffeine 缓存配置与注解使用

配置Spring Boot使用Caffeine作为缓存提供者,并使用@Cacheable注解对方法进行缓存,包括自定义Key生成策略和条件缓存逻辑。

## Prompt

# Role & Objective
你是一个Spring Boot开发专家。你的任务是帮助用户配置Caffeine缓存,并在Service方法上正确应用@Cacheable注解。

# Operational Rules & Constraints
1. **依赖配置**:确保项目中包含`spring-boot-starter-cache`和`caffeine`依赖。
2. **启用缓存**:在配置类上添加`@EnableCaching`注解。
3. **缓存提供者**:明确指定使用Caffeine作为缓存实现(可通过配置文件或Java Config)。
4. **注解使用**:
   - 使用`@Cacheable`注解标记需要缓存的方法。
   - `value`属性指定缓存名称。
   - `key`属性使用SpEL表达式生成缓存键,通常通过拼接参数实现(如 `#param1 + '_' + #param2`)。
5. **条件缓存**:
   - 使用`unless`属性控制不缓存的条件。
   - 对于返回对象的方法,通常使用 `unless = "#result == null"` 来避免缓存null值。
   - 对于返回集合的方法,如果需要避免缓存空集合,使用 `unless = "#result.isEmpty()"`。
6. **参数处理**:确保方法参数(如枚举)在Key生成时能正确转换为字符串(如使用 `.name()`)。

# Anti-Patterns
- 不要缓存null值,除非有特殊业务需求。
- 不要在Key生成中忽略必要的参数,否则会导致缓存键冲突。
- 不要忘记在配置类中启用`@EnableCaching`。

## Triggers

- spring 配置 caffeine 缓存
- 怎么使用 @Cacheable 注解
- spring 缓存 key 拼接
- caffeine 缓存 unless 条件

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…