Tuesday 20 April 2021

Guava Cache: Hello world application

In this post, I am going to explain,

a.   How to create a cache using Guava CacheBuilder

b.   How to store the object into the cache

c.    How to retrieve the objects from cache

 

How to create a cache?

You can create a cache using CacheBuilder class.

 

Example

Cache<Integer, Employee> empCache = CacheBuilder.newBuilder().maximumSize(500).expireAfterAccess(15, TimeUnit.SECONDS).expireAfterWrite(30, TimeUnit.SECONDS).build();

 

How to add an object to the cache?

Using put method, you can add a <key,value> pair into the cache.

 

Example

empCache.put(1, new Employee(1, "Krishna", "Gurram"));

 

How to retrieve an element from the cache?

Using ‘getIfPresent’ method, you can retrieve an element from the cache.

 

Example

Employee emp1 = empCache.getIfPresent(1);

If there is an entry exist in the cache for the key 1, this method return the object, else null.

 

Find the below working application.

 

Employee.java 

package com.sample.app.cache.model;

public class Employee {
	private Integer id;
	private String firstName;
	private String lastName;

	public Employee(Integer id, String firstName, String lastName) {
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}

}

 

HelloWorld.java

package com.sample.app.cache;

import java.util.concurrent.TimeUnit;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.sample.app.cache.model.Employee;

public class HelloWorld {
	private static void printEmployee(Employee emp, Integer id) {
		if (emp == null) {
			System.out.println("Employee not exist for the id " + id);
			return;
		}
		System.out.println(emp);
	}

	public static void main(String args[]) throws InterruptedException {
		Cache<Integer, Employee> empCache = CacheBuilder.newBuilder().maximumSize(500)
				.expireAfterAccess(15, TimeUnit.SECONDS).expireAfterWrite(30, TimeUnit.SECONDS).build();

		empCache.put(1, new Employee(1, "Krishna", "Gurram"));
		empCache.put(2, new Employee(2, "Sailu", "PTR"));

		System.out.println("Getting employee with ids 1, 2 and 3 from empCache");
		Employee emp1 = empCache.getIfPresent(1);
		Employee emp2 = empCache.getIfPresent(2);
		Employee emp3 = empCache.getIfPresent(3);

		printEmployee(emp1, 1);
		printEmployee(emp2, 2);
		printEmployee(emp3, 3);

		System.out.println("\nGoing to sleep for 35 seconds\n");
		TimeUnit.SECONDS.sleep(35);

		emp1 = empCache.getIfPresent(1);
		emp2 = empCache.getIfPresent(2);
		emp3 = empCache.getIfPresent(3);

		System.out.println("Getting employee with ids 1, 2 and 3 from empCache");
		printEmployee(emp1, 1);
		printEmployee(emp2, 2);
		printEmployee(emp3, 3);

	}

}

 

Output

Getting employee with ids 1, 2 and 3 from empCache
Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=2, firstName=Sailu, lastName=PTR]
Employee not exist for the id 3

Going to sleep for 35 seconds

Getting employee with ids 1, 2 and 3 from empCache
Employee not exist for the id 1
Employee not exist for the id 2
Employee not exist for the id 3

 



Previous                                                    Next                                                    Home

No comments:

Post a Comment