Spring 4 + Guava Cache Integration Example with GuavaCacheManager and GuavaCache

By Arvind Rai, November 15, 2015
This page will provide Spring 4 and Guava cache integration example with GuavaCacheManager and GuavaCache. GuavaCacheManager provides the cache using Google guava cache which provides a cache with optimization. In case we want more than one cache with different optimization using Google guava cache, we can use spring GuavaCache. Find the complete example step by step, one for GuavaCacheManager and second for GuavaCache.

Software Used

Find the software used in demo.
1. Java 8
2. Spring 4
3. Gradle
4. Eclipse

build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'SpringCache'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.springframework.boot:spring-boot-starter:1.2.7.RELEASE'
    compile 'org.springframework:spring-context-support:4.1.8.RELEASE'
    compile 'com.google.guava:guava:19.0-rc2' 
}  

Using GuavaCacheManager

org.springframework.cache.guava.GuavaCacheManager lazily builds org.springframework.cache.guava.GuavaCache instances. We can optimize spring cache by using Google guava cache.
AppConfigA.java
package com.concretepage; 
import java.util.concurrent.TimeUnit;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.google.common.cache.CacheBuilder;
@Configuration
@ComponentScan("com.concretepage")
@EnableCaching
public class AppConfigA  {
    @Bean
    public CacheManager cacheManager() {
       GuavaCacheManager cacheManager = new GuavaCacheManager("mycache");
       CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder()
       .maximumSize(100)
       .expireAfterWrite(10, TimeUnit.MINUTES);
       cacheManager.setCacheBuilder(cacheBuilder);
       return cacheManager;
    }
} 
In this case we can define a cache with one configuration.
Book.java
package com.concretepage;
public class Book {
	private String bookName;
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
} 

BookAppA.java
package com.concretepage;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class BookAppA {
	Book book = new Book();
	@Cacheable(value = "mycache")
	public Book getBook() {
		System.out.println("Executing getBook method...");
		book.setBookName("Mahabharat");
		return book;
	}
} 

CacheDemoA.java
package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class CacheDemoA {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		ctx.register(AppConfigA.class);
		ctx.refresh();
		BookAppA bookAppA = ctx.getBean(BookAppA.class);
		//Calling getBook method first time.
		System.out.println(bookAppA.getBook().getBookName());
		//Calling getBook method second time.
		//This time, method will not execute because result is cached with "mycache"
		System.out.println(bookAppA.getBook().getBookName());
		ctx.close();
	}
} 
Find the output.
Executing getBook method...
Mahabharat
Mahabharat 

Using GuavaCache : Define more than one Cache with Different Optimization

Using org.springframework.cache.guava.GuavaCache, we can define more than one cache with different configuration.
AppConfigB.java
package com.concretepage; 
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.google.common.cache.CacheBuilder;
@Configuration
@ComponentScan("com.concretepage")
@EnableCaching
public class AppConfigB  {
    @Bean
    public CacheManager cacheManager() {
       SimpleCacheManager cacheManager = new SimpleCacheManager();
       GuavaCache guavaCache1 = new GuavaCache("book", CacheBuilder.newBuilder()
    		   .maximumSize(50).build());
       GuavaCache guavaCache2 = new GuavaCache("bookstore", CacheBuilder.newBuilder()
    		   .maximumSize(100).expireAfterAccess(5, TimeUnit.MINUTES).build());
       cacheManager.setCaches(Arrays.asList(guavaCache1, guavaCache2));
       return cacheManager;
    }
} 

BookStore.java
package com.concretepage;
public class BookStore {
	private String bookStoreName;
	public String getBookStoreName() {
		return bookStoreName;
	}
	public void setBookStoreName(String bookStoreName) {
		this.bookStoreName = bookStoreName;
	}
} 

BookAppB.java
package com.concretepage;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class BookAppB {
	Book book = new Book();
	BookStore bookStore = new BookStore();
	@Cacheable(value = "book")
	public Book getBook() {
		System.out.println("Executing getBook method...");
		book.setBookName("Mahabharat");
		return book;
	}
	@Cacheable(value = "bookstore")
	public BookStore getBookStore() {
		System.out.println("Executing getBookStore method...");
		bookStore.setBookStoreName("Sri Krishna Book Store");
		return bookStore;
	}
} 

CacheDemoB.java
package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class CacheDemoB {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		ctx.register(AppConfigB.class);
		ctx.refresh();
		BookAppB bookAppB = ctx.getBean(BookAppB.class);
		//Calling getBook method first time.
		System.out.println(bookAppB.getBook().getBookName());
		//Calling getBook method second time.
		//This time, method will not execute because result is cached with "book"
		System.out.println(bookAppB.getBook().getBookName());
                System.out.println("---------------------------");
		//Calling getBookStoreName method first time.
		System.out.println(bookAppB.getBookStore().getBookStoreName());
		//Calling getBookStoreName method second time.
		//This time, method will not execute because result is cached with "bookstore"
		System.out.println(bookAppB.getBookStore().getBookStoreName());
		ctx.close();
	}
} 
Find the output.
Executing getBook method...
Mahabharat
Mahabharat
---------------------------
Executing getBookStore method...
Sri Krishna Book Store
Sri Krishna Book Store 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us