高德地圖一面:聊聊 @Cacheable 注解的原理!
在 Spring 框架中,@Cacheable注解是什么?它有什么用途?它是如何工作的?這篇文章,我們來聊一聊。@Cacheable注解
一、@Cacheable概述
首先,我們看看@Conditional注解的源碼,截圖如下:
通過源碼可以知道:@Cacheable表示可以緩存調(diào)用某個(gè)方法(或某個(gè)類中的所有方法)的結(jié)果的注解,它可以用在類和方法上。更具體地說,@Cacheable用于將方法的結(jié)果緩存起來,如果遇到方法并且參數(shù)都完全相同的情況,會(huì)直接從緩存中獲取結(jié)果,而無需執(zhí)行方法體。
@Cacheable 的工作原理如下:
(1) 第一次調(diào)用:調(diào)用被 @Cacheable 注解的方法時(shí),Spring 會(huì)先檢查緩存中是否存在對(duì)應(yīng)的緩存條目。
- 如果不存在,方法會(huì)被執(zhí)行,且返回的結(jié)果會(huì)被存入緩存中。
- 如果存在,方法不會(huì)被執(zhí)行,直接返回緩存中的結(jié)果。
(2) 后續(xù)調(diào)用:每次調(diào)用時(shí),Spring 都會(huì)基于方法的參數(shù)在緩存中查找對(duì)應(yīng)的條目,存在則直接返回緩存結(jié)果,避免了重復(fù)計(jì)算或訪問數(shù)據(jù)源。
二、@Cacheable 的使用
下面,我們將通過詳細(xì)的示例來介紹 @Cacheable 的使用方法。
1.. 添加依賴
首先,我們需要在項(xiàng)目中添加 Spring 緩存相關(guān)的依賴,比如,我們使用 Spring Boot 和 Redis 作為緩存實(shí)現(xiàn),這里以 Maven為例:
<!-- Maven 依賴 -->
<dependencies>
<!-- Spring Boot Starter Cache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Redis 作為緩存實(shí)現(xiàn) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
2. 啟用緩存
在 Spring Boot應(yīng)用的啟動(dòng)類或配置類上添加 @EnableCaching 注解,以啟用緩存支持。示例代碼如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching // 開啟緩存支持
public class CacheableDemoApplication {
public static void main(String[] args) {
SpringApplication.run(CacheableDemoApplication.class, args);
}
}
3. 使用 @Cacheable 注解
我們可以在需要緩存的方法上添加 @Cacheable 注解,并指定緩存名稱。示例代碼如下:
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
publicclass UserService {
// 假設(shè)這是一個(gè)耗時(shí)的查詢方法,比如從數(shù)據(jù)庫中獲取用戶信息
@Cacheable(cacheNames = "users", key = "#userId")
public User getUserById(Long userId) {
simulateSlowService(); // 模擬慢服務(wù)
returnnew User(userId, "User" + userId);
}
private void simulateSlowService() {
try {
Thread.sleep(3000L); // 模擬3秒延遲
} catch (InterruptedException e) {
thrownew IllegalStateException(e);
}
}
}
在上述代碼中:
- cacheNames = "users":指定緩存的名稱為 users。可以理解為緩存的命名空間。
- key = "#userId":指定緩存的鍵為方法參數(shù) userId 的值。
4. 測(cè)試緩存效果
下面,我們通過調(diào)用getUserById方法兩次,第一次會(huì)經(jīng)過延遲,第二次將直接從緩存中獲取來進(jìn)行測(cè)試。示例代碼如下:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
publicclass CacheTestRunner implements CommandLineRunner {
@Autowired
private UserService userService;
@Override
public void run(String... args) throws Exception {
long start = System.currentTimeMillis();
User user1 = userService.getUserById(1L); // 第一次查詢,耗時(shí)
long end = System.currentTimeMillis();
System.out.println("First call took: " + (end - start) + "ms");
start = System.currentTimeMillis();
User user2 = userService.getUserById(1L); // 第二次查詢,從緩存獲取,快速
end = System.currentTimeMillis();
System.out.println("Second call took: " + (end - start) + "ms");
}
}
運(yùn)行結(jié)果類似于:
First call took: 3005ms
Second call took: 15ms
說明第一次調(diào)用執(zhí)行了方法體并緩存了結(jié)果,第二次調(diào)用則直接從緩存中獲取。
三、屬性詳解
@Cacheable 注解提供了多個(gè)屬性,以便更靈活地控制緩存行為,如下源碼截圖:
下面,我們將對(duì)主要屬性進(jìn)行詳細(xì)的說明。
1. cacheNames/value
- 描述:指定緩存的名稱,可以是一個(gè)或多個(gè)。
- 類型:String[]
- 默認(rèn)值:無
說明:cacheNames 和 value 是同義屬性,通常使用 cacheNames。指定一個(gè)緩存名稱相當(dāng)于指定一個(gè)命名空間,可以在配置緩存管理器時(shí)對(duì)不同名稱的緩存指定不同的配置。
@Cacheable(cacheNames = "users")
// 或
@Cacheable(value = "users")
2. key
- 描述:指定緩存的鍵。在 SpEL(Spring Expression Language)表達(dá)式中,可以使用方法參數(shù)、返回值等。
- 類型:String
- 默認(rèn)值:基于參數(shù)的所有方法參數(shù)生成的鍵,類似于 SimpleKey 機(jī)制。
@Cacheable(cacheNames = "users", key = "#userId")
@Cacheable(cacheNames = "users", key = "#root.methodName + #userId")
- #userId:使用 userId 參數(shù)作為鍵。
- #a0 或 #p0:使用第一個(gè)參數(shù)作為鍵。
- #result.id:使用方法返回值的 id 屬性作為鍵(適用于 key 屬性中的 unless)。
3. keyGenerator
- 描述:指定自定義的鍵生成器的名稱。與 key 屬性互斥。
- 類型:String
- 默認(rèn)值:"cacheKeyGenerator",即使用配置的默認(rèn)鍵生成器。
@Cacheable(cacheNames = "users", keyGenerator = "myKeyGenerator")
自定義鍵生成器示例:
@Component("myKeyGenerator")
public class MyKeyGenerator implements KeyGenerator {
@Override
public Object generate(Object target, Method method, Object... params) {
return method.getName() + "_" + Arrays.stream(params).map(Object::toString).collect(Collectors.joining("_"));
}
}
4. cacheManager
- 描述:指定用于該注解的緩存管理器的名稱。
- 類型:String
- 默認(rèn)值:使用配置的默認(rèn) CacheManager。
@Cacheable(cacheNames = "users", cacheManager = "cacheManager1")
5. cacheResolver
- 描述:指定緩存解析器,優(yōu)先級(jí)高于 cacheManager 和 cacheNames。
- 類型:String
- 默認(rèn)值:無
@Cacheable(cacheResolver = "myCacheResolver")
6. condition
- 描述:使用 SpEL 表達(dá)式進(jìn)行條件判斷,決定是否緩存。只有表達(dá)式結(jié)果為 true 時(shí),才進(jìn)行緩存。
- 類型:String
- 默認(rèn)值:""(總是緩存)
@Cacheable(cacheNames = "users", condition = "#userId > 10")
上述示例中,只有當(dāng) userId 大于 10 時(shí),才緩存結(jié)果。
7. unless
- 描述:與 condition 相反,用來決定是否不緩存。僅當(dāng)表達(dá)式結(jié)果為 true 時(shí),不進(jìn)行緩存。
- 類型:String
- 默認(rèn)值:""(不阻止緩存)
@Cacheable(cacheNames = "users", unless = "#result == null")
上述示例中,只有當(dāng)方法返回結(jié)果為 null 時(shí),不緩存。
8. sync
- 描述:是否啟用同步緩存。默認(rèn)值為 false。
- 類型:boolean
- 默認(rèn)值:false
當(dāng)多個(gè)線程同時(shí)請(qǐng)求尚未緩存的值時(shí),啟用同步緩存可以防止多線程重復(fù)加載緩存。
@Cacheable(cacheNames = "users", sync = true)
9. 綜合示例
@Cacheable(
cacheNames = "users",
key = "#userId",
condition = "#userId > 10",
unless = "#result == null",
sync = true
)
public User getUserById(Long userId) {
// 方法實(shí)現(xiàn)
}
- 緩存名稱為 users
- 鍵為 userId
- 僅當(dāng) userId 大于 10 時(shí)緩存
- 如果返回結(jié)果為 null,則不緩存
- 啟用同步緩存,防止緩存穿透導(dǎo)致的高并發(fā)請(qǐng)求重復(fù)加載
四、配置緩存管理器
要使用 @Cacheable,需要配置一個(gè) CacheManager,Spring 提供了多種緩存管理器的實(shí)現(xiàn),如 ConcurrentMapCacheManager(基于本地 ConcurrentHashMap)、RedisCacheManager、EhCacheCacheManager 等。
1. 使用默認(rèn)的 ConcurrentMapCacheManager
如果沒有特別指定,Spring Boot 會(huì)默認(rèn)使用 ConcurrentMapCacheManager。適用于簡單的開發(fā)和測(cè)試場(chǎng)景。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
@Configuration
public class CacheConfig {
@Bean
public ConcurrentMapCacheManager cacheManager() {
return new ConcurrentMapCacheManager("users", "products");
}
}
2. 使用 Redis 作為緩存實(shí)現(xiàn)
Redis 是一個(gè)高性能的內(nèi)存數(shù)據(jù)庫,適用于分布式應(yīng)用的緩存需求。
(1) 配置 Redis 連接
在 application.properties 或 application.yml 中配置 Redis 連接信息。
spring.redis.host=localhost
spring.redis.port=6379
(2) 配置 RedisCacheManager
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import java.time.Duration;
@Configuration
publicclass RedisCacheConfig {
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(60)) // 設(shè)置緩存過期時(shí)間
.disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return RedisCacheManager.builder(connectionFactory)
.cacheDefaults(config)
.build();
}
}
說明:
- entryTtl(Duration.ofMinutes(60)):設(shè)置緩存的默認(rèn)過期時(shí)間為 60 分鐘。
- disableCachingNullValues():不緩存 null 值。
- serializeValuesWith:配置緩存值的序列化方式,建議使用 JSON 序列化,便于調(diào)試和跨語言兼容。
3. 多個(gè)緩存管理器
你可以配置多個(gè) CacheManager,并通過 cacheManager 屬性在 @Cacheable 注解中指定使用哪個(gè)緩存管理器。例如:
@Configuration
public class MultipleCacheConfig {
@Bean
public CacheManager cacheManager1(RedisConnectionFactory connectionFactory) {
// RedisCacheManager 配置
}
@Bean
public CacheManager cacheManager2() {
// ConcurrentMapCacheManager 配置
}
}
在 @Cacheable 中指定:
@Cacheable(cacheNames = "users", cacheManager = "cacheManager1")
public User getUserById(Long userId) {
// 方法實(shí)現(xiàn)
}
五、總結(jié)
本文,我們從源碼角度深度分析了 @Cacheable注解,Spring通過該注解提供了一種簡潔且強(qiáng)大的緩存處理方式。在實(shí)際工作中,我們一定要根據(jù)實(shí)際情況來選擇合適的緩存策略,另外,在使用緩存的同時(shí),我們也需要注意緩存常見的問題,比如穿透、擊穿和雪崩,并采取相應(yīng)的解決措施。