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);
117 * Method for deleted cached items for a given cache key
121 public void deleteCacheItems(String cacheKey)
123 cacheItems.put(cacheKey, new LinkedHashSet<String>());
124 persistCache(cacheKey);
128 * Method for obtaining the preset maximum cache limit for a given cache key
133 public String getCacheLmit(String cacheKey)
135 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
136 return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
140 * Method for updating the preset maximum cache limit for a given cache key
146 public int updateCacheLimit(String cacheKey, String newLimit)
148 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
149 String formerLimit = getCacheLmit(cacheKey);
150 if (newLimit != null && !newLimit.isEmpty()
151 && !formerLimit.equals(newLimit))
153 Cache.setProperty(uniqueKey, newLimit);
154 formerLimit = newLimit;
156 return Integer.valueOf(formerLimit);
160 * Method for inserting cache items for given cache key into the cache data
166 public void putCache(String cacheKey, LinkedHashSet<String> cacheItems)
168 getCacheItems().put(cacheKey, cacheItems);
172 * Getter method for obtaining cache data structure
176 private Hashtable<String, LinkedHashSet<String>> getCacheItems()