import java.util.HashMap; import java.util.Map; /** * Template for read cache class. * @author Ronald Koster */ public class ReadCache { private static ReadCache cache; private Map map; //Requires thread-safe access. private long timestamp; //Idem. private ReadCache() {} public static synchronized ReadCache getInstance() { if (cache == null) { cache = new ReadCache(); } return cache; } /** * @return The cached data. Usually a database table (key: primary key, value: row object). Or a map * of such maps, in which case the keys in the main map are the table names. */ public synchronized Map getMap() { if (this.map == null || System.currentTimeMillis() - this.timestamp > getRefreshPeriod()) { init(); } return this.map; } public synchronized void refresh() { init(); } private void init() { Map newMap = new HashMap(); //TODO: load newMap from database. this.timestamp = System.currentTimeMillis(); this.map = newMap; } private long getRefreshPeriod() { return 10 * 60 * 1000; //TODO: fetch this from a property file. } }