1 package jalview.io.cache;
4 import jalview.bin.Cache;
6 import java.util.Hashtable;
7 import java.util.LinkedHashSet;
10 * A singleton class used for querying and persisting cache items.
17 public static final String DEFAULT_LIMIT = "99";
19 public static final String CACHE_DELIMITER = ";";
21 private static AppCache instance = null;
23 private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
27 private Hashtable<String, LinkedHashSet<String>> cacheItems;
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 a singleton instance of AppCache
57 public static AppCache getInstance()
61 instance = new AppCache();
69 * Method for persisting cache items for a given cache key
73 public void persistCache(String cacheKey)
75 LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
76 StringBuffer delimitedCacheBuf = new StringBuffer();
77 for (String cacheItem : foundCacheItems)
79 delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
81 if (delimitedCacheBuf.length() > 0)
83 delimitedCacheBuf.deleteCharAt(0);
85 String delimitedCacheString = delimitedCacheBuf.toString();
87 Cache.setProperty(cacheKey, delimitedCacheString);
91 * Method for deleting cached items for a given cache key
96 public void deleteCacheItems(String cacheKey)
98 cacheItems.put(cacheKey, new LinkedHashSet<String>());
99 persistCache(cacheKey);
103 * Method for obtaining the preset maximum cache limit for a given cache key
107 * @return the max number of items that could be cached
109 public String getCacheLimit(String cacheKey)
111 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
112 return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
116 * Method for updating the preset maximum cache limit for a given cache key
121 * the max number of items that could be cached for the given cache
125 public int updateCacheLimit(String cacheKey, int newUserLimit)
127 String newLimit = String.valueOf(newUserLimit);
128 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
129 String formerLimit = getCacheLimit(cacheKey);
130 if (newLimit != null && !newLimit.isEmpty()
131 && !formerLimit.equals(newLimit))
133 Cache.setProperty(uniqueKey, newLimit);
134 formerLimit = newLimit;
136 return Integer.valueOf(formerLimit);
140 * Method for inserting cache items for given cache key into the cache data
146 * the items to add to the cache
148 public void putCache(String cacheKey, LinkedHashSet<String> newCacheItems)
150 cacheItems.put(cacheKey, newCacheItems);