1 package jalview.io.cache;
3 import jalview.bin.Cache;
5 import java.util.Hashtable;
6 import java.util.LinkedHashSet;
9 * A singleton class used for querying and persisting cache items.
16 public static final String DEFAULT_LIMIT = "99";
18 public static final String CACHE_DELIMITER = ";";
20 private static AppCache instance = null;
22 private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
24 private Hashtable<String, LinkedHashSet<String>> cacheItems;
28 cacheItems = new Hashtable<String, LinkedHashSet<String>>();
32 * Method to obtain all the cache items for a given cache key
37 public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
39 LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
40 if (foundCache == null)
42 foundCache = new LinkedHashSet<String>();
43 cacheItems.put(cacheKey, foundCache);
49 * Returns a singleton instance of AppCache
53 public static AppCache getInstance()
57 instance = new AppCache();
63 * Method for persisting cache items for a given cache key
67 public void persistCache(String cacheKey)
69 LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
70 StringBuffer delimitedCacheBuf = new StringBuffer();
71 for (String cacheItem : foundCacheItems)
73 delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
75 if (delimitedCacheBuf.length() > 0)
77 delimitedCacheBuf.deleteCharAt(0);
79 String delimitedCacheString = delimitedCacheBuf.toString();
81 Cache.setProperty(cacheKey, delimitedCacheString);
85 * Method for deleting cached items for a given cache key
90 public void deleteCacheItems(String cacheKey)
92 cacheItems.put(cacheKey, new LinkedHashSet<String>());
93 persistCache(cacheKey);
97 * Method for obtaining the preset maximum cache limit for a given cache key
101 * @return the max number of items that could be cached
103 public String getCacheLimit(String cacheKey)
105 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
106 return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
110 * Method for updating the preset maximum cache limit for a given cache key
115 * the max number of items that could be cached for the given cache
119 public int updateCacheLimit(String cacheKey, int newUserLimit)
121 String newLimit = String.valueOf(newUserLimit);
122 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
123 String formerLimit = getCacheLimit(cacheKey);
124 if (newLimit != null && !newLimit.isEmpty()
125 && !formerLimit.equals(newLimit))
127 Cache.setProperty(uniqueKey, newLimit);
128 formerLimit = newLimit;
130 return Integer.valueOf(formerLimit);
134 * Method for inserting cache items for given cache key into the cache data
140 * the items to add to the cache
142 public void putCache(String cacheKey, LinkedHashSet<String> newCacheItems)
144 cacheItems.put(cacheKey, newCacheItems);