使用Guava配置Spring缓存

时间:2015-02-03 10:39:21

标签: java spring caching guava

spring documentation about cache之后,我可以在项目中使用缓存,但是如何配置guava来定义每个缓存名称的过期时间或大小?

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) {
    ...
}

5 个答案:

答案 0 :(得分:27)

您可以单独配置缓存。见Spring Guava cache

@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;
}

答案 1 :(得分:17)

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

  1. 如果是Java配置,它可能如下所示:
  2. @Bean
    public CacheManager cacheManager() {
        GuavaCacheManager cacheManager = new GuavaCacheManager();
        cacheManager.setCacheBuilder(
            CacheBuilder.
            newBuilder().
            expireAfterWrite(2, TimeUnit.SECONDS).
            maximumSize(100));
        return cacheManager;
    }
    
    1. 如果是XML配置,您可以在guava中使用CacheBuilderSpec
    2. <bean id="legendaryCacheBuilder"
            class="com.google.common.cache.CacheBuilder"
            factory-method="from">
          <constructor-arg value="maximumSize=42,expireAfterAccess=10m,expireAfterWrite=1h" />
      </bean>
      

      有关详细信息,请参阅:

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

      Injecting Google guava cache builder into bean via Spring

答案 2 :(得分:2)

另一种方式

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>

爪哇

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

答案 3 :(得分:1)

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

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

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());
    }
}

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

@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;
}

答案 4 :(得分:0)

通过扩展 'CachingConfigurerSupport' 类,您可以提供自定义缓存定义。

例如

@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {
    @Bean
    @Override
    public CacheManager cacheManager() {
    
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        GuavaCache myOrgCache = new GuavaCache("myOrgCache", CacheBuilder.newBuilder().build());
        GuavaCache myEmployeeCache = new GuavaCache("myEmployeeCache",
                CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).build());
        cacheManager.setCaches(Arrays.asList(myOrgCache, myEmployeeCache));
        return cacheManager;
    }

    @Override
    public KeyGenerator keyGenerator() {
        return new CacheKeyGenerator();
    }
}

参考link