package jalview.io.cache; import jalview.bin.Cache; import java.util.Arrays; import java.util.Hashtable; import java.util.LinkedHashSet; import java.util.List; /** * A singleton class used for querying and persisting cache items. * * @author tcnofoegbu * */ public class AppCache { private static AppCache instance = null; private Hashtable> cacheItems; private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT"; private static final String DEFAULT_LIMIT = "99"; private static final String CACHE_DELIMITER = ";"; private AppCache() { cacheItems = new Hashtable>(); } /** * Method to obtain all the cache items for a given cache key * * @param cacheKey * @return */ public LinkedHashSet getAllCachedItemsFor(String cacheKey) { LinkedHashSet foundCache = cacheItems.get(cacheKey); if (foundCache == null) { foundCache = new LinkedHashSet(); cacheItems.put(cacheKey, foundCache); } return foundCache; } /** * Returns an singleton instance of AppCache * * @return */ public static AppCache getInstance() { if (instance == null) { instance = new AppCache(); } return instance; } /** * Method for initialising cache items for a given cache key * * @param cacheKey */ public void initCache(String cacheKey) { String separatedStr = Cache.getProperty(cacheKey); if (separatedStr == null || separatedStr.isEmpty()) { return; } List persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER)); LinkedHashSet foundCacheItems = cacheItems.get(cacheKey); if (foundCacheItems == null) { foundCacheItems = new LinkedHashSet(); } for (String cacheItem : persistedCacheItems) { foundCacheItems.add(cacheItem); } cacheItems.put(cacheKey, foundCacheItems); } /** * Method for persisting cache items for a given cache key * * @param cacheKey */ public void persistCache(String cacheKey) { LinkedHashSet foundCacheItems = getAllCachedItemsFor(cacheKey); StringBuffer delimitedCacheBuf = new StringBuffer(); for (String cacheItem : foundCacheItems) { delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem); } if (delimitedCacheBuf.length() > 0) { delimitedCacheBuf.deleteCharAt(0); } String delimitedCacheString = delimitedCacheBuf.toString(); Cache.setProperty(cacheKey, delimitedCacheString); } public void deleteCacheItems(String cacheKey) { cacheItems.put(cacheKey, new LinkedHashSet()); persistCache(cacheKey); } public String getCacheLmit(String cacheKey) { String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY; return Cache.getDefault(uniqueKey, DEFAULT_LIMIT); } public int updateCacheLimit(String cacheKey, String newLimit) { String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY; String formerLimit = getCacheLmit(cacheKey); if (newLimit != null && !newLimit.isEmpty() && !formerLimit.equals(newLimit)) { Cache.setProperty(uniqueKey, newLimit); formerLimit = newLimit; } return Integer.valueOf(formerLimit); } /** * Method for inserting cache items for given cache key into the cache data * structure * * @param cacheKey * @param cacheItems */ public void putCache(String cacheKey, LinkedHashSet cacheItems) { getCacheItems().put(cacheKey, cacheItems); } /** * Getter method for obtaining cache data structure * * @return */ private Hashtable> getCacheItems() { return cacheItems; } }