Tuesday 27 April 2021

caffeine cache: Hello world

Step 1: Define Cache instance.

 

Cache<Integer, Employee> cache = Caffeine.newBuilder().expireAfterWrite(30, TimeUnit.SECONDS).maximumSize(100)

.build();

 

Above snippet defines a cache instance, which store Employee object by their ids as key. This information exists in the cache for 30 seconds and evicted after that.

 

Step 2: Add some entries to the cache using ‘put’ method.

 

void put(K key, V value)

Associates the value with the key in this cache. If the cache previously contained a value associated with the key, the old value is replaced by the new value.

 

Example

cache.put(emp1.getId(), emp1);

 

Step 2: Use ‘getIfPresent’ method of Cache interface to get the entry associated with given key.

 

V getIfPresent(K key)

Returns the value associated with the key in this cache, or null if there is no cached value for the key.

 

Example

Employee emp = cache.getIfPresent(1);

 

Find the below working application.

 

Employee.java

package com.sample.app.model;

public class Employee {

	private int id;
	private String firstName;
	private String lastName;

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

	public int getId() {
		return id;
	}

	public void setId(int 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 + "]";
	}

}

 

App.java

package com.sample.app;

import java.util.concurrent.TimeUnit;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.sample.app.model.Employee;

public class App {

	public static void main(String args[]) {
		Cache<Integer, Employee> cache = Caffeine.newBuilder().expireAfterWrite(30, TimeUnit.SECONDS).maximumSize(100)
				.build();

		Employee emp1 = new Employee(1, "Krishna", "Gurram");
		Employee emp2 = new Employee(2, "Gopi", "Battu");
		Employee emp3 = new Employee(3, "Saurav", "Sarkar");

		cache.put(emp1.getId(), emp1);
		cache.put(emp2.getId(), emp2);
		cache.put(emp3.getId(), emp3);

		System.out.println("Retrieving the employee information with id 1");
		Employee emp = cache.getIfPresent(1);
		System.out.println(emp);

		System.out.println("\nRetrieving the employee information with id 4");
		emp = cache.getIfPresent(4);
		System.out.println(emp);

	}

}

 

Output

 

Retrieving the employee information with id 1
Employee [id=1, firstName=Krishna, lastName=Gurram]

Retrieving the employee information with id 4
null

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment