package jalview.io.cache;
+import jalview.bin.Cache;
+
+import java.util.Arrays;
import java.util.Hashtable;
import java.util.LinkedHashSet;
+import java.util.List;
import java.util.Set;
public class AppCache implements AppCacheI
private Hashtable<String, LinkedHashSet<String>> cacheItems;
+ private static final String CACHE_DELIMITER = ";";
+
private AppCache()
{
cacheItems = new Hashtable<String, LinkedHashSet<String>>();
}
@Override
- public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
+ public LinkedHashSet<String> getAllCachedItemsFor(Cacheable cacheable)
{
+ String cacheKey = cacheable.getCacheKey();
LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
if (foundCache == null)
{
if (userInput != null && !userInput.isEmpty())
{
- LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheKey);
+ LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheable);
foundCache.add(userInput);
cacheItems.put(cacheKey, foundCache);
}
cacheComboBox.removeAllItems();
}
- Set<String> cacheItems = getAllCachedItemsFor(cacheKey);
+ Set<String> cacheItems = getAllCachedItemsFor(cacheable);
if (cacheItems != null && !cacheItems.isEmpty())
{
for (String cacheItem : cacheItems)
}
else
{
- cacheable.init();
+ cacheable.initCache();
cacheComboBox.addItem("");
cacheComboBox.setSelectedItem("");
}
}
+ @Override
+ public void initCache(Cacheable cacheable)
+ {
+ String separatedStr = Cache.getProperty(cacheable.getCacheKey());
+ if (separatedStr == null || separatedStr.isEmpty())
+ {
+ return;
+ }
+
+ List<String> persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER));
+ String cacheKey = cacheable.getCacheKey();
+
+ LinkedHashSet<String> foundCacheItems = cacheItems.get(cacheKey);
+ if (foundCacheItems == null)
+ {
+ foundCacheItems = new LinkedHashSet<String>();
+ }
+
+ for (String cacheItem : persistedCacheItems)
+ {
+ foundCacheItems.add(cacheItem);
+ }
+ cacheItems.put(cacheKey, foundCacheItems);
+ updateCache(cacheable);
+ }
+
+ @Override
+ public void persistCache(Cacheable cacheable)
+ {
+ String cacheKey = cacheable.getCacheKey();
+ LinkedHashSet<String> foundCache = getAllCachedItemsFor(cacheable);
+ String commaJoinedStr = String.join(CACHE_DELIMITER, foundCache);
+ Cache.setProperty(cacheKey, commaJoinedStr);
+ }
}