Java 使用 Guava 配置 Spring 缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28296839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:58:01  来源:igfitidea点击:

Config Spring cache using Guava

javaspringcachingguava

提问by xedo

Following the spring documentation about cacheI could use cache on my project, but how can I configure guava to define a expired time or size per cache name?

按照有关缓存spring 文档,我可以在我的项目中使用缓存,但是如何配置番石榴来定义每个缓存名称的过期时间或大小?

applicationConfig.xml

应用程序配置文件

<bean id="cacheManager" class="org.springframework.cache.guava.GuavaCacheManager"/>

Foo.java

文件

@Cacheable(value="courses", key="#user.id")
public List<Course> getCoursesByUser(User user) {
    ...
}

回答by mavarazy

You can specify CacheBuilder for your GuavaCacheManager in your Spring configuration

您可以在 Spring 配置中为 GuavaCacheManager 指定 CacheBuilder

  1. In case of Java configuration it can look like this:
  1. 在 Java 配置的情况下,它可能如下所示:
@Bean
public CacheManager cacheManager() {
    GuavaCacheManager cacheManager = new GuavaCacheManager();
    cacheManager.setCacheBuilder(
        CacheBuilder.
        newBuilder().
        expireAfterWrite(2, TimeUnit.SECONDS).
        maximumSize(100));
    return cacheManager;
}
  1. In case of XML configuration, you can use CacheBuilderSpec in guava
  1. 在 XML 配置的情况下,您可以在番石榴中使用 CacheBuilderSpec
<bean id="legendaryCacheBuilder"
      class="com.google.common.cache.CacheBuilder"
      factory-method="from">
    <constructor-arg value="maximumSize=42,expireAfterAccess=10m,expireAfterWrite=1h" />
</bean>

For more information look at:

有关更多信息,请查看:

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/CacheBuilderSpec.html

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/CacheBuilderSpec.html

Injecting Google guava cache builder into bean via Spring

通过 Spring 将 Google guava 缓存构建器注入到 bean 中

回答by Kyrylo Semenko

You can configure caches separately. See Spring Guava cache

您可以单独配置缓存。查看Spring Guava 缓存

@Bean
public CacheManager cacheManager() {
    SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
    GuavaCache bookCache = new GuavaCache("book", CacheBuilder.newBuilder().build());
    GuavaCache booksExpirableCache = new GuavaCache("books", CacheBuilder.newBuilder()
            .expireAfterAccess(30, TimeUnit.MINUTES)
            .build());
    simpleCacheManager.setCaches(Arrays.asList(bookCache, booksExpirableCache));
    return simpleCacheManager;
}

回答by Jaikrat

In another way

用另一种方式

XML

XML

   <bean id="cacheManager" class="org.springframework.cache.guava.GuavaCacheManager">
        <property name="cacheBuilderSpec">
            <bean class="com.google.common.cache.CacheBuilderSpec" factory-method="parse">
                <constructor-arg name="cacheBuilderSpecification" value="expireAfterWrite=55m"/>
            </bean>
        </property>
    </bean>

Java

爪哇

@Cacheable(value = "tokenValue", cacheManager = "cacheManager")

回答by Przemek Nowak

I think that @mavarazy answer is the best. I only add if you need you own automatic missed cache configuration you could do it on the following way.

我认为@mavarazi 的答案是最好的。我只添加如果您需要自己的自动错过的缓存配置,您可以通过以下方式进行。

First define you own cache manager which creates automatically cache if you need it:

首先定义你自己的缓存管理器,如果你需要它会自动创建缓存:

public class MyCacheManager extends SimpleCacheManager {

    @Override
    protected Cache getMissingCache(String name) {
        // or different cache config if you need
        return new GuavaCache(name, CacheBuilder.newBuilder().maximumSize(250).expireAfterWrite(10, TimeUnit.MINUTES).build());
    }
}

And now you can define cache manager configuration:

现在您可以定义缓存管理器配置:

@Bean
public CacheManager cacheManager() {
    SimpleCacheManager simpleCacheManager = new MyCacheManager();
    GuavaCache specificCacheConfig = new GuavaCache("specificCacheConfigName",
        CacheBuilder.newBuilder().expireAfterAccess(60, TimeUnit.MINUTES).build());
    simpleCacheManager.setCaches(Collections.singletonList(specificCacheConfig));
    return simpleCacheManager;
}