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 DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
25 private static final String DEFAULT_LIMIT = "99";
27 private static final String CACHE_DELIMITER = ";";
31 cacheItems = new Hashtable<String, LinkedHashSet<String>>();
35 * Method to obtain all the cache items for a given cache key
40 public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
42 LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
43 if (foundCache == null)
45 foundCache = new LinkedHashSet<String>();
46 cacheItems.put(cacheKey, foundCache);
53 * Returns an singleton instance of AppCache
57 public static AppCache getInstance()
61 instance = new AppCache();
67 * Method for initialising cache items for a given cache key
71 public void initCache(String cacheKey)
73 String separatedStr = Cache.getProperty(cacheKey);
74 if (separatedStr == null || separatedStr.isEmpty())
79 List<String> persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER));
81 LinkedHashSet<String> foundCacheItems = cacheItems.get(cacheKey);
82 if (foundCacheItems == null)
84 foundCacheItems = new LinkedHashSet<String>();
87 for (String cacheItem : persistedCacheItems)
89 foundCacheItems.add(cacheItem);
91 cacheItems.put(cacheKey, foundCacheItems);
95 * Method for persisting cache items for a given cache key
99 public void persistCache(String cacheKey)
101 LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
102 StringBuffer delimitedCacheBuf = new StringBuffer();
103 for (String cacheItem : foundCacheItems)
105 delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
107 if (delimitedCacheBuf.length() > 0)
109 delimitedCacheBuf.deleteCharAt(0);
111 String delimitedCacheString = delimitedCacheBuf.toString();
113 Cache.setProperty(cacheKey, delimitedCacheString);
116 public void deleteCacheItems(String cacheKey)
118 cacheItems.put(cacheKey, new LinkedHashSet<String>());
119 persistCache(cacheKey);
122 public String getCacheLmit(String cacheKey)
124 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
125 return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
128 public int updateCacheLimit(String cacheKey, String newLimit)
130 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
131 String formerLimit = getCacheLmit(cacheKey);
132 if (newLimit != null && !newLimit.isEmpty()
133 && !formerLimit.equals(newLimit))
135 Cache.setProperty(uniqueKey, newLimit);
136 formerLimit = newLimit;
138 return Integer.valueOf(formerLimit);
142 * Method for inserting cache items for given cache key into the cache data
148 public void putCache(String cacheKey, LinkedHashSet<String> cacheItems)
150 getCacheItems().put(cacheKey, cacheItems);
154 * Getter method for obtaining cache data structure
158 private Hashtable<String, LinkedHashSet<String>> getCacheItems()