Monday 4 October 2021

ExpiringMap: Set the maximum cache size

Using 'maxSize' method, we can set the maximum size to expiring cache.

 

Example

Map<Integer, Employee> expiringMap = ExpiringMap.builder().expiration(10, TimeUnit.SECONDS).maxSize(5).expirationPolicy(ExpirationPolicy.CREATED).build();

 

Once this size has been reached, adding an additional entry will expire the first entry in line for expiration based on the expiration policy. Let’s confirm with below example.

 

Employee.java

 

package com.sample.app.model;

public class Employee {

	private int id;
	private String name;

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

	public int getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

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

}

 

MaxCacheSizeDemo.java

package com.sample.app;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.sample.app.model.Employee;

import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;

public class MaxCacheSizeDemo {

	private static void sleepNSeconds(int n) throws InterruptedException {
		System.out.println("\nSleeping for " + n + " seconds");
		TimeUnit.SECONDS.sleep(n);
	}

	private static void printCache(Map<Integer, Employee> expiringMap) {

		System.out.println("\nPrinting all the elements of cache");
		expiringMap.forEach((key, value) -> {
			System.out.println("Key : " + key + " Value : " + value);
		});
	}

	public static void main(String args[]) throws InterruptedException {
		Map<Integer, Employee> expiringMap = ExpiringMap.builder().expiration(10, TimeUnit.SECONDS).maxSize(5)
				.expirationPolicy(ExpirationPolicy.CREATED).build();

		System.out.println("Adding 5 employees");

		for (int i = 1; i < 6; i++) {
			Employee emp = new Employee(i, "emp " + i);
			expiringMap.put(i, emp);
		}

		sleepNSeconds(3);

		printCache(expiringMap);

		System.out.println("\nAdd two more elements");
		expiringMap.put(6, new Employee(6, "emp6"));
		expiringMap.put(7, new Employee(7, "emp7"));

		printCache(expiringMap);
	}
}

 

Output

Adding 5 employees

Sleeping for 3 seconds

Printing all the elements of cache
Key : 1 Value : Employee [id=1, name=emp 1]
Key : 2 Value : Employee [id=2, name=emp 2]
Key : 3 Value : Employee [id=3, name=emp 3]
Key : 4 Value : Employee [id=4, name=emp 4]
Key : 5 Value : Employee [id=5, name=emp 5]

Add two more elements

Printing all the elements of cache
Key : 3 Value : Employee [id=3, name=emp 3]
Key : 4 Value : Employee [id=4, name=emp 4]
Key : 5 Value : Employee [id=5, name=emp 5]
Key : 6 Value : Employee [id=6, name=emp6]
Key : 7 Value : Employee [id=7, name=emp7]

 

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment