有两个类,都用
@Cacheable
(org.springframework.cache.annotation.Cacheable
) 注释,要么是方法,要么是类。我认为这个问题并不重要。
示例:
@Cacheable(value = STATE_CACHE_NAME, key = "#root.methodName + '-' + #country?.id ?: \"null\" + '-' + #stateName ?: \"null\"")
public Long getStateId(CountryEntity country, String stateName) {...}
在某些课程中,我想使用简单的内存缓存,而其他课程则使用 Redis 缓存。我如何表达这个意图?
@Cacheable
采用参数“cacheManager”,它可以让您指定它引用的是 CacheManager
的哪个 bean。
因此,您可以将其与您想要的CacheManager
的
bean名称一起添加。
@Cacheable(cacheManager = "inMemoryCache", value = STATE_CACHE_NAME, key = "#root.methodName + '-' + #country?.id ?: \"null\" + '-' + #stateName ?: \"null\"")
public Long getStateId(CountryEntity country, String
您可以使用 bean 方法的名称,也可以通过
@Bean
的“name”参数提供名称。
例如
@Bean(name = "inMemoryCache")
CacheManager cacheManager() {
}