Sunday, May 19, 2013

Using Spring-Cache-Guava

 Spring-Cache-Guava, uses the Maven build system. Building the code yourself should be a straightforward case of:
  git clone
  cd spring-cache-guava
  mvn install

To use Spring-Cache-Guava, you need to include the runtime module in the dependencies section of your pom.xml (replacing ${spring-cache-guava.version} with the appropriate current release):


Instances of GuavaCache can be created using Spring XML configuration:

This code snippet above creates an instance of the Guava cache factory bean using the default configuration. You can fine-tune the cache instance properties by providing a CacheBuilderSpec as follows:

GavaCaches need to be wrapped into a CacheManager to be used with Spring Cache Abstraction. Spring-Cache-Guava has a CacheManager that can be used to this end:


Now you have your CacheManager configured and Spring Framework caching annotations enabled. All you have to do is to create your beans and annotate them using Spring Framework Cache annotations.

  @Cacheable(value = "users-cache")
  public User findUserByUserName(String userName) {
    return userRepository.findByUserName(checkNotNull(userName));


I had some free time recently and wrote a small adapter for Spring Framework Cache Abstraction backed by Google Guava Cache. I use both frameworks extensively and enjoy the features they provide.

The source code is available on GitHub. The Introduction page explains how to build the artifacts and use them in your project. You can use the Issues page to submit bugs / requests.

I will post more entries on this blog to explain more and share few lessons I learned.

Dear Blog, I'm so sorry

Few years passed since I last wrote something on this blog... It's time to start blogging again...

I never had any issues with Blogger, so instead of starting a new blog somewhere else,
I thought I'd just continue posting here.