2017-06-15 103 views
0

我有一個Spring Boot Web應用程序,並使用spring會話與redis存儲。 Web請求有時需要緩存他們的響應(以避免不必要的數據庫訪問),並且我計劃使用咖啡因。
然而,似乎Redis接管了(只要我包含gradle依賴)作爲緩存實現,因爲我爲咖啡因設置的所有TTL都被忽略。並行Spring Boot多緩存管理器

在Spring Boot應用程序中使用超過1個Cache提供程序甚至可能/推薦嗎? 我可以嘗試將Redis用於所有緩存,只是擔心它會影響Spring Boot附帶的會話實現(我沒有配置任何使用@EnableRedisHttpSession的內容)。

我很欣賞這方面的任何建議。

+1

有一個[檢測順序] (https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html#_supported_cache_providers)。我的理解是Spring Cache旨在允許多個緩存管理器並且這樣做被認爲是可以的。 –

回答

1

您可以使用單獨的緩存經理@Cacheable

@RequestMapping(value = "/hello/{name}", method = RequestMethod.GET) 
@Cacheable(key = "#name", cacheManager = "caffeineCacheManager") 
public String greeet(@PathVariable String name) { 
    return "Hello " + name; 
} 

,你唯一需要的是讓你的緩存管理器爲一個名爲豆:

@Bean 
@Qualifier("caffeineCacheManager") 
AbstractCacheManager caffeineCacheManager() { 
    return new CaffeineCacheManager(); 
}