这是spring引导的源代码:
/**
* Create a native Guava Cache instance for the specified cache name.
* @param name the name of the cache
* @return the native Guava Cache instance
*/
protected com.google.common.cache.Cache<Object, Object> createNativeGuavaCache(String name) {
if (this.cacheLoader != null) {
return this.cacheBuilder.build(this.cacheLoader);
}
else {
return this.cacheBuilder.build();
}
}缓存的所有名称只有一个缓存如何创建两个不同的缓存?
发布于 2018-11-23 18:32:57
您可以在春季使用不同的GuavaCache创建多个expiry time,如下所示:
import com.google.common.cache.CacheBuilder;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
@Configuration
@EnableCaching
public class CacheConfiguration extends CachingConfigurerSupport {
private static final int TIME_TO_LEAVE = 120; //in minutes
private static final int CACHE_SIZE = 100;
public static final String CACHE1 = "cache1";
public static final String CACHE2 = "cache2";
@Bean
@Override
public CacheManager cacheManager() {
SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
GuavaCache cache1 = new GuavaCache(CACHE1,
CacheBuilder.newBuilder()
.expireAfterWrite(TIME_TO_LEAVE, TimeUnit.MINUTES)
.maximumSize(CACHE_SIZE)
.build());
GuavaCache cache2 = new GuavaCache(CACHE2,
CacheBuilder.newBuilder()
.expireAfterWrite(TIME_TO_LEAVE, TimeUnit.MINUTES)
.maximumSize(CACHE_SIZE)
.build());
simpleCacheManager.setCaches(Arrays.asList(cache1, cache2));
return simpleCacheManager;
}
}您可以访问这些缓存,只需用Cacheable注释注释一个方法,并以value的形式提供缓存名称:
@Cacheable(value = CacheConfiguration.CACHE1)
int getAge(Person person){
}https://stackoverflow.com/questions/42241211
复制相似问题