Showing posts with label ExpiringMap. Show all posts
Showing posts with label ExpiringMap. Show all posts

Monday, 4 October 2021

ExpiringMap: Reset expiration time of the entry

Using 'resetExpiration' method, we can reset the expiration time of the entry.

 

Example

expiringMap.resetExpiration(1);

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

}


ResetExpirationTimeDemo.java

package com.sample.app;

import java.util.concurrent.TimeUnit;

import com.sample.app.model.Employee;

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

public class ResetExpirationTimeDemo {

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

	public static void main(String args[]) throws InterruptedException {

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

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

		long expectedExpiration1 = expiringMap.getExpectedExpiration(1);
		System.out.println("Expected expiry time for entry1 " + expectedExpiration1 +  " milli seconds");
		
		expiringMap.resetExpiration(1);
		
		System.out.println("\nResetting the expiration time of the entry 1");

		expectedExpiration1 = expiringMap.getExpectedExpiration(1);
		System.out.println("\nExpected expiry time for entry1 " + expectedExpiration1 +  " milli seconds");
	}
}


Output

Sleeping for 2 seconds
Expected expiry time for entry1 2981 milli seconds

Resetting the expiration time of the entry 1

Expected expiry time for entry1 4999 milli seconds



Previous                                                    Next                                                    Home

ExpiringMap: Get expected expiration time of a key

Using 'getExpectedExpiration' method, we can get the expected expiration time. This method return the expected expiration time in milliseconds.

 

Example

long expectedExpiration1 = expiringMap.getExpectedExpiration(1);

 

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

}

 

ExpectedExpirationDemo.java

package com.sample.app;

import java.util.concurrent.TimeUnit;

import com.sample.app.model.Employee;

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

public class ExpectedExpirationDemo {

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

	public static void main(String args[]) throws InterruptedException {

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

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

		expiringMap.put(2, new Employee(2, "Employee" + 2));

		long expectedExpiration1 = expiringMap.getExpectedExpiration(1);
		long expectedExpiration2 = expiringMap.getExpectedExpiration(2);

		System.out.println("Expected exiry time for entry1 " + expectedExpiration1 +  " milli seconds");
		System.out.println("Expected exiry time for entry2 " + expectedExpiration2 + " milli seconds");

	}
}

 

Output

Sleeping for 2 seconds
Expected exiry time for entry1 2981 milli seconds
Expected exiry time for entry2 4999 milli seconds

 

 


Previous                                                    Next                                                    Home

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

ExpiringMap: Remove expiration listener at run time

Expiration listeners can also be added and removed on the fly using 'addExpirationListener' and 'removeExpirationListener' methods.

 

Example

expiringMap.removeExpirationListener(empListener);

 

Find the below working application.

 

RemoveExpirationListenerDemo.java

package com.sample.app;

import java.util.concurrent.TimeUnit;

import com.sample.app.model.Employee;

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

public class RemoveExpirationListenerDemo {

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

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

		expiringMap.put(1, new Employee(1, "Ram"));

		System.out.println(("Adding expiration listener to the ExpiringMap"));
		ExpirationListener<Integer, Employee> empListener = new ExpirationListener<Integer, Employee>() {

			@Override
			public void expired(Integer key, Employee emp) {
				emp.logOnExpire();
			}
		};
		expiringMap.addExpirationListener(empListener);
		sleepNSeconds(7);

		System.out.println("\nAdding employee entry again");
		expiringMap.put(1, new Employee(1, "Ram"));

		System.out.println("\nRemoving the listener");
		expiringMap.removeExpirationListener(empListener);

		sleepNSeconds(10);

	}
}

Output

Adding expiration listener to the ExpiringMap

Sleeping for 7 seconds
Employee [id=1, name=Ram] is expired

Adding employee entry again

Removing the listener

Sleeping for 10 seconds




 

  

Previous                                                    Next                                                    Home

ExpiringMap: Add expiration listener at run time

Expiration listeners can also be added and removed on the fly using 'addExpirationListener' and 'removeExpirationListener' methods.

 

Example

expiringMap.addExpirationListener((key, employee) -> ((Employee) employee).logOnExpire());

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

}


AddListenerDemo.java

package com.sample.app;

import java.util.concurrent.TimeUnit;

import com.sample.app.model.Employee;

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

public class AddListenerDemo {

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

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

		expiringMap.put(1, new Employee(1, "Ram"));
		
		System.out.println(("Adding expiration listener to the ExpiringMap"));
		expiringMap.addExpirationListener((key, employee) -> ((Employee) employee).logOnExpire());
		sleepNSeconds(10);

	}
}


Output

Adding expiration listener to the ExpiringMap

Sleeping for 10 seconds
Employee [id=1, name=Ram] is expired


  

Previous                                                    Next                                                    Home