I am trying to implement an in-memory cache.
The cache is populated during startup, the map inside the enum can be updated any time during the lifetime of the application.
What can be the impact of Garbage collection on this implementation? Since the enum is like a static class I am assuming that only one instance of the map object is created and contents of this map are not garbage collected.
We have seen that sometimes keys from this map go missing can this be because of GC?
We are returning a new map whenever a call is made to retrieve this cache
public enum EmployeeCache {
INSTANCE;
private Map<String,Map<String, Employee>> employeeCacheMap = new ConcurrentHashMap<>();
public void populateEmployeeCache(String tenantId,Map<String, Employee> inputEmployeeCacheMap){
Map<String, Employee> localEmployeeCacheMap = new ConcurrentHashMap<>(inputEmployeeCacheMap);
employeeCacheMap.put(tenantId,localEmployeeCacheMap);
}
public Map<String, Employee> getEmployeeCacheMap(String tenantId){
Map<String,Employee> localMap = new HashMap<>();
if(employeeCacheMap.get(tenantId) != null) {
localMap.putAll(employeeCacheMap.get(tenantId));
}
return localMap;
}
public Employee getEmployeeInfo(String tenantId,String Employee){
return employeeCacheMap.get(tenantId).get(Employee);
}
}