Tuesday 27 April 2021

Caffeine cache: Record statistics

Step 1: Define Cache instance by enabling recordStats.

 

Cache<Integer, Employee> cache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).recordStats().build();

 

Step 2: Get CacheStats instance and work with it.

CacheStats cacheStats = cache.stats();

 

long evictionCount = cacheStats.evictionCount();

long hitCount = cacheStats.hitCount();

long missCount = cacheStats.missCount();

 

Find the below working application.

 

Employee.java 

package com.sample.app.model;

public class Employee {

	private int id;
	private String firstName;
	private String lastName;
	private long expiryTime;

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

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

	public long getExpiryTime() {
		return expiryTime;
	}

	public void setExpiryTime(long expiryTime) {
		this.expiryTime = expiryTime;
	}

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

}

 

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.github.benmanes.caffeine.cache.stats.CacheStats;
import com.sample.app.model.Employee;

public class App {

	private static void printCacheStats(Cache cache) {
		CacheStats cacheStats = cache.stats();

		long evictionCount = cacheStats.evictionCount();
		long hitCount = cacheStats.hitCount();
		long missCount = cacheStats.missCount();

		System.out.println("--------------------------------------");
		System.out.println("\nCache statistics");
		System.out.println("evictionCount : " + evictionCount);
		System.out.println("hitCount : " + hitCount);
		System.out.println("missCount : " + missCount);
		System.out.println("--------------------------------------");
	}

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

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

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

		System.out.println(cache.getIfPresent(1));
		System.out.println(cache.getIfPresent(11));

		printCacheStats(cache);

		System.out.println("\nSleeping for 10 seconds");
		TimeUnit.SECONDS.sleep(10);
		
		cache.cleanUp();

		printCacheStats(cache);

	}

}

 

Output

Employee [id=1, firstName=Krishna, lastName=Gurram, expiryTime=5]
null
--------------------------------------

Cache statistics
evictionCount : 0
hitCount : 1
missCount : 1
--------------------------------------

Sleeping for 10 seconds
--------------------------------------

Cache statistics
evictionCount : 3
hitCount : 1
missCount : 1
--------------------------------------

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment