This post talks about How to implement LRU Cache in Java
What is LRU (Least Recently Used) Cache?
When the Cache reaches its maximum capacity, then the least recently used item should be invalided before inserting a new Item into cache.
Below is the LRU Implementation
Here Employee object we are putting as key in the LRU cache and Department object as value.
Department.Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.infotech.model; public class Department { private Integer deptId; private String deptName; private String deptLocation; public Department(Integer deptId, String deptName, String deptLocation) { super(); this.deptId = deptId; this.deptName = deptName; this.deptLocation = deptLocation; } @Override public String toString() { return "Department [deptId=" + deptId + ", deptName=" + deptName + ", deptLocation=" + deptLocation + "]"; } } |
Employee.Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
package com.infotech.model; public class Employee { private Integer employeeId; private String employeeName; private Double salary; private String email; public Employee(Integer employeeId, String employeeName, Double salary, String email) { super(); this.employeeId = employeeId; this.employeeName = employeeName; this.salary = salary; this.email = email; } public Integer getEmployeeId() { return employeeId; } public String getEmployeeName() { return employeeName; } public Double getSalary() { return salary; } public String getEmail() { return email; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((email == null) ? 0 : email.hashCode()); result = prime * result + ((employeeId == null) ? 0 : employeeId.hashCode()); result = prime * result + ((employeeName == null) ? 0 : employeeName.hashCode()); result = prime * result + ((salary == null) ? 0 : salary.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; if (email == null) { if (other.email != null) return false; } else if (!email.equals(other.email)) return false; if (employeeId == null) { if (other.employeeId != null) return false; } else if (!employeeId.equals(other.employeeId)) return false; if (employeeName == null) { if (other.employeeName != null) return false; } else if (!employeeName.equals(other.employeeName)) return false; if (salary == null) { if (other.salary != null) return false; } else if (!salary.equals(other.salary)) return false; return true; } @Override public String toString() { return "Employee [employeeId=" + employeeId + ", employeeName=" + employeeName + ", salary=" + salary + ", email=" + email + "]"; } } |
LRUCache.java
Below is the LRU cache implementation in java using Generics
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.infotech.cache; import java.util.LinkedHashMap; import java.util.Map; public class LRUCache<K, V> extends LinkedHashMap<K, V> { private static final long serialVersionUID = -4622177786359540728L; private int size; private LRUCache(int size) { super(size, 0.75f, true); this.size = size; } @Override protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > size; } public static <K, V> LRUCache<K, V> newInstance(int size) { return new LRUCache<K, V>(size); } } |
ClientTest.java
Here is the client program to test LRU cache.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.infotech.client; import java.util.Map.Entry; import java.util.Set; import com.infotech.cache.LRUCache; import com.infotech.model.Department; import com.infotech.model.Employee; public class ClientTest { public static void main(String[] args) { Department d1 = new Department(10001, "IT", "Boston"); Department d2 = new Department(10002, "Finance", "Chennai"); LRUCache<Employee, Department> cache = LRUCache.newInstance(3); cache.put(e1, d1); cache.put(e2, d1); cache.put(e3, d1); cache.put(e4, d2); cache.put(e5, d2); Set<Entry<Employee, Department>> entrySet = cache.entrySet(); for (Entry<Employee, Department> entry : entrySet) { Employee employee = entry.getKey(); Department department = entry.getValue(); System.out.println(employee); System.out.println(department); } } } |
You May Also Like:
What will happen if in a try block we throw an exception but in the finally block we return a int value?
Adding two numbers without using arithmetic operators?
If you have any feedback or suggestion please feel free to drop in below comment box.