1 package jalview.io.cache;
4 import jalview.bin.Cache;
6 import java.util.Arrays;
7 import java.util.Hashtable;
8 import java.util.LinkedHashSet;
12 * A singleton class used for querying and persisting cache items.
19 private static AppCache instance = null;
21 private Hashtable<String, LinkedHashSet<String>> cacheItems;
23 private static final String CACHE_DELIMITER = ";";
27 cacheItems = new Hashtable<String, LinkedHashSet<String>>();
31 * Method to obtain all the cache items for a given cache key
36 public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
38 LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
39 if (foundCache == null)
41 foundCache = new LinkedHashSet<String>();
42 cacheItems.put(cacheKey, foundCache);
49 * Returns an singleton instance of AppCache
53 public static AppCache getInstance()
57 instance = new AppCache();
63 * Method for initialising cache items for a given cache key
67 public void initCache(String cacheKey)
69 String separatedStr = Cache.getProperty(cacheKey);
70 if (separatedStr == null || separatedStr.isEmpty())
75 List<String> persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER));
77 LinkedHashSet<String> foundCacheItems = cacheItems.get(cacheKey);
78 if (foundCacheItems == null)
80 foundCacheItems = new LinkedHashSet<String>();
83 for (String cacheItem : persistedCacheItems)
85 foundCacheItems.add(cacheItem);
87 cacheItems.put(cacheKey, foundCacheItems);
91 * Method for persisting cache items for a given cache key
95 public void persistCache(String cacheKey)
97 LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
98 StringBuffer delimitedCacheBuf = new StringBuffer();
99 for (String cacheItem : foundCacheItems)
101 delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
103 if (delimitedCacheBuf.length() > 0)
105 delimitedCacheBuf.deleteCharAt(0);
107 String delimitedCacheString = delimitedCacheBuf.toString();
109 Cache.setProperty(cacheKey, delimitedCacheString);
112 public void deleteCacheItems(String cacheKey)
114 cacheItems.put(cacheKey, new LinkedHashSet<String>());
115 persistCache(cacheKey);
119 * Method for inserting cache items for given cache key into the cache data
125 public void putCache(String cacheKey, LinkedHashSet<String> cacheItems)
127 getCacheItems().put(cacheKey, cacheItems);
131 * Getter method for obtaining cache data structure
135 private Hashtable<String, LinkedHashSet<String>> getCacheItems()