Monday, 4 October 2021

ExpiringMap: Load the entries lazily

Using 'entryLoader' method, you can lazily load the objects when the object is not found in ExpiringMap.

 

Example

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

 

Find the below working application.

 

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 + "]";
	}
	
	public void logOnExpire() {
		System.out.println(this.toString() + " is expired");
	}

}

 

LazyLoadingDemo.java

package com.sample.app;

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

import com.sample.app.model.Employee;

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

public class LazyLoadingDemo {
	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 {

		EntryLoader<Integer, Employee> entryLoader = new EntryLoader<Integer, Employee>() {

			@Override
			public Employee load(Integer key) {
				System.out.println("\nEmployee with the key " + key + " is not exist, loading lazily.....");
				return new Employee(key, "Employee " + key);
			}

		};

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

		expiringMap.put(1, new Employee(1, "Employee" + 1));
		printCache(expiringMap);

		System.out.println("Trying to get the entries for employee 2 and 3");

		System.out.println(expiringMap.get(2));
		System.out.println(expiringMap.get(3));

		printCache(expiringMap);
	}
}

 

Output

Printing all the elements of cache
Key : 1 Value : Employee [id=1, name=Employee1]
Trying to get the entries for employee 2 and 3

Employee with the key 2 is not exist, loading lazily.....
Employee [id=2, name=Employee 2]

Employee with the key 3 is not exist, loading lazily.....
Employee [id=3, name=Employee 3]

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

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment