X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2Fcache%2FAppCache.java;h=eaf6ecd65b25423572a18958c5287a29ad1de902;hb=e134764b7eec841cb56a417250f2dd898680f985;hp=9f9a25b37c2b3d9c3d01ae5675e223dc8e82dca1;hpb=221f613b5d235a11c602b989598afe97b5b7b203;p=jalview.git diff --git a/src/jalview/io/cache/AppCache.java b/src/jalview/io/cache/AppCache.java index 9f9a25b..eaf6ecd 100644 --- a/src/jalview/io/cache/AppCache.java +++ b/src/jalview/io/cache/AppCache.java @@ -1,12 +1,29 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.io.cache; - import jalview.bin.Cache; -import java.util.Arrays; import java.util.Hashtable; import java.util.LinkedHashSet; -import java.util.List; /** * A singleton class used for querying and persisting cache items. @@ -16,11 +33,15 @@ import java.util.List; */ public class AppCache { + public static final String DEFAULT_LIMIT = "99"; + + public static final String CACHE_DELIMITER = ";"; + private static AppCache instance = null; - private Hashtable> cacheItems; + private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT"; - private static final String CACHE_DELIMITER = ";"; + private Hashtable> cacheItems; private AppCache() { @@ -44,9 +65,8 @@ public class AppCache return foundCache; } - /** - * Returns an singleton instance of AppCache + * Returns a singleton instance of AppCache * * @return */ @@ -60,34 +80,6 @@ public class AppCache } /** - * Method for initialising cache items for a given cache key - * - * @param cacheKey - */ - public void initCache(String cacheKey) - { - String separatedStr = Cache.getProperty(cacheKey); - if (separatedStr == null || separatedStr.isEmpty()) - { - return; - } - - List persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER)); - - LinkedHashSet foundCacheItems = cacheItems.get(cacheKey); - if (foundCacheItems == null) - { - foundCacheItems = new LinkedHashSet(); - } - - for (String cacheItem : persistedCacheItems) - { - foundCacheItems.add(cacheItem); - } - cacheItems.put(cacheKey, foundCacheItems); - } - - /** * Method for persisting cache items for a given cache key * * @param cacheKey @@ -109,6 +101,12 @@ public class AppCache Cache.setProperty(cacheKey, delimitedCacheString); } + /** + * Method for deleting cached items for a given cache key + * + * @param cacheKey + * the cache key + */ public void deleteCacheItems(String cacheKey) { cacheItems.put(cacheKey, new LinkedHashSet()); @@ -116,24 +114,54 @@ public class AppCache } /** - * Method for inserting cache items for given cache key into the cache data - * structure + * Method for obtaining the preset maximum cache limit for a given cache key * * @param cacheKey - * @param cacheItems + * the cache key + * @return the max number of items that could be cached */ - public void putCache(String cacheKey, LinkedHashSet cacheItems) + public String getCacheLimit(String cacheKey) { - getCacheItems().put(cacheKey, cacheItems); + String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY; + return Cache.getDefault(uniqueKey, DEFAULT_LIMIT); } /** - * Getter method for obtaining cache data structure + * Method for updating the preset maximum cache limit for a given cache key * + * @param cacheKey + * the cache key + * @param newLimit + * the max number of items that could be cached for the given cache + * key * @return */ - private Hashtable> getCacheItems() + public int updateCacheLimit(String cacheKey, int newUserLimit) { - return cacheItems; + String newLimit = String.valueOf(newUserLimit); + String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY; + String formerLimit = getCacheLimit(cacheKey); + if (newLimit != null && !newLimit.isEmpty() + && !formerLimit.equals(newLimit)) + { + Cache.setProperty(uniqueKey, newLimit); + formerLimit = newLimit; + } + return Integer.valueOf(formerLimit); } + + /** + * Method for inserting cache items for given cache key into the cache data + * structure + * + * @param cacheKey + * the cache key + * @param cacheItems + * the items to add to the cache + */ + public void putCache(String cacheKey, LinkedHashSet newCacheItems) + { + cacheItems.put(cacheKey, newCacheItems); + } + }