Spring Configuration Using Guava

Following the spring cache documentation , I could use the cache in my project, but how can I configure guava to determine the elapsed time or size for each cache name?

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) { ... } 
+7
java spring caching guava
source share
4 answers

You can configure caches separately. See 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; } 
+25
source share

You can specify a CacheBuilder for your GuavaCacheManager in the Spring configuration

  • In the case of a Java configuration, it might look like this:
 @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
 <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 see:

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

Implementing Google Guava Cache Builder in a bean via Spring

+14
source share

Differently

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") 
+1
source share

I think @mavarazy's answer is the best. I add only if you need automatic configuration of the missing cache, you can do it as follows.

First, define your own cache manager, which creates an automatic 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 the 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; } 
+1
source share

All Articles