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

Post a Comment