2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.io.cache;
23 import jalview.bin.Cache;
24 import jalview.bin.ApplicationSingletonProvider;
25 import jalview.bin.ApplicationSingletonProvider.ApplicationSingletonI;
27 import java.util.Hashtable;
28 import java.util.LinkedHashSet;
31 * A singleton class used for querying and persisting cache items.
37 public class AppCache implements ApplicationSingletonI
40 public static AppCache getInstance()
42 return (AppCache) ApplicationSingletonProvider.getInstance(AppCache.class);
47 cacheItems = new Hashtable<>();
50 public static final String DEFAULT_LIMIT = "99";
52 public static final String CACHE_DELIMITER = ";";
54 private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
56 private Hashtable<String, LinkedHashSet<String>> cacheItems;
59 * Method to obtain all the cache items for a given cache key
64 public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
66 LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
67 if (foundCache == null)
69 foundCache = new LinkedHashSet<>();
70 cacheItems.put(cacheKey, foundCache);
76 * Method for persisting cache items for a given cache key
80 public void persistCache(String cacheKey)
82 LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
83 StringBuffer delimitedCacheBuf = new StringBuffer();
84 for (String cacheItem : foundCacheItems)
86 delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
88 if (delimitedCacheBuf.length() > 0)
90 delimitedCacheBuf.deleteCharAt(0);
92 String delimitedCacheString = delimitedCacheBuf.toString();
94 Cache.setProperty(cacheKey, delimitedCacheString);
98 * Method for deleting cached items for a given cache key
103 public void deleteCacheItems(String cacheKey)
105 cacheItems.put(cacheKey, new LinkedHashSet<String>());
106 persistCache(cacheKey);
110 * Method for obtaining the preset maximum cache limit for a given cache key
114 * @return the max number of items that could be cached
116 public String getCacheLimit(String cacheKey)
118 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
119 return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
123 * Method for updating the preset maximum cache limit for a given cache key
128 * the max number of items that could be cached for the given cache
132 public int updateCacheLimit(String cacheKey, int newUserLimit)
134 String newLimit = String.valueOf(newUserLimit);
135 String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
136 String formerLimit = getCacheLimit(cacheKey);
137 if (newLimit != null && !newLimit.isEmpty()
138 && !formerLimit.equals(newLimit))
140 Cache.setProperty(uniqueKey, newLimit);
141 formerLimit = newLimit;
143 return Integer.valueOf(formerLimit);
147 * Method for inserting cache items for given cache key into the cache data
153 * the items to add to the cache
155 public void putCache(String cacheKey, LinkedHashSet<String> newCacheItems)
157 cacheItems.put(cacheKey, newCacheItems);