Tuesday 27 April 2021

Caffeine: size based eviction

Caffeine evict the items when the configured size limit of the cache is exceeded.

 

Example

Cache<Integer, Employee> cache = Caffeine.newBuilder().initialCapacity(1).maximumSize(2).expireAfterWrite(30, TimeUnit.SECONDS).build();

 

As you see above snippet, I set the maximum cache size to 2, when you try to insert 3rd item to the cache, the least recently used record is deleted from the cache.

 

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.Map;
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 {
	
	private static void printCache(Cache cache) {
		System.out.println("\n\nElements of cache are");
		Map map = cache.asMap();
		
		for(Object key : map.keySet()) {
			System.out.println(map.get(key));
		}
	}

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

		TimeUnit.SECONDS.sleep(2);

		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);
		
		printCache(cache);

		long estimatedSize = cache.estimatedSize();
		System.out.println("Estimated number of elements in the cache : " + estimatedSize);

		/*
		 * Since cache eviction is done asynchronously, let's execute pending
		 * maintenance operations needed by the cache
		 */
		System.out.println("\nPerforming cleanup operations\n");
		cache.cleanUp();
		
		printCache(cache);

		estimatedSize = cache.estimatedSize();
		System.out.println("Estimated number of elements in the cache : " + estimatedSize);
	}

}

 

Output

Elements of cache are
Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=2, firstName=Gopi, lastName=Battu]
Employee [id=3, firstName=Saurav, lastName=Sarkar]
Estimated number of elements in the cache : 2

Performing cleanup operations



Elements of cache are
Employee [id=2, firstName=Gopi, lastName=Battu]
Employee [id=3, firstName=Saurav, lastName=Sarkar]
Estimated number of elements in the cache : 2

 

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment