如何在同一个项目中使用内存和redis缓存?

问题描述 投票:0回答:1

有两个类,都用

@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 缓存。我如何表达这个意图?

java spring caching redis in-memory
1个回答
0
投票

@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() {
}
© www.soinside.com 2019 - 2024. All rights reserved.