import java.util.Collections; import java.util.HashMap; import java.util.Map; /** * * This class maintains in memory cache. * @author kamalmeet * */ public class CommonCache { //The map that will store the cache private static Map<String, Object> cache=null; private static CommonCache reportCache=new CommonCache(); /** * Make sure the class is singleton so only one instance is shared by all. */ private CommonCache() { cache=Collections.synchronizedMap(new HashMap<String, Object>()); } /** * Get the singleton instance. * @return */ public static CommonCache getInstance() { return reportCache; } /** * Add new element to cache. * @param key * @param value */ public void addToCache(String key, Object value) { cache.put(key, value); } /** * Remove an element from cache. * @param key */ public void removeFromCache(String key) { cache.remove(key); } /** * Clean up cache. */ public void invalidateCache() { cache=Collections.synchronizedMap(new HashMap<String, Object>()); } /** * Get the element from cache. * @param key * @return */ public Object getFromCache(String key) { if(cache.containsKey(key)) return cache.get(key); else return null; } }