JAL-2484 added improvement to enable clearing cache items and configuring maximum...
[jalview.git] / src / jalview / io / cache / AppCache.java
1 package jalview.io.cache;
2
3
4 import jalview.bin.Cache;
5
6 import java.util.Arrays;
7 import java.util.Hashtable;
8 import java.util.LinkedHashSet;
9 import java.util.List;
10
11 /**
12  * A singleton class used for querying and persisting cache items.
13  * 
14  * @author tcnofoegbu
15  *
16  */
17 public class AppCache
18 {
19   private static AppCache instance = null;
20
21   private Hashtable<String, LinkedHashSet<String>> cacheItems;
22
23   private static final String DEFAULT_LIMIT_KEY = ".DEFAULT_LIMIT";
24
25   private static final String DEFAULT_LIMIT = "99";
26
27   private static final String CACHE_DELIMITER = ";";
28
29   private AppCache()
30   {
31     cacheItems = new Hashtable<String, LinkedHashSet<String>>();
32   }
33
34   /**
35    * Method to obtain all the cache items for a given cache key
36    * 
37    * @param cacheKey
38    * @return
39    */
40   public LinkedHashSet<String> getAllCachedItemsFor(String cacheKey)
41   {
42     LinkedHashSet<String> foundCache = cacheItems.get(cacheKey);
43     if (foundCache == null)
44     {
45       foundCache = new LinkedHashSet<String>();
46       cacheItems.put(cacheKey, foundCache);
47     }
48     return foundCache;
49   }
50
51
52   /**
53    * Returns an singleton instance of AppCache
54    * 
55    * @return
56    */
57   public static AppCache getInstance()
58   {
59     if (instance == null)
60     {
61       instance = new AppCache();
62     }
63     return instance;
64   }
65
66   /**
67    * Method for initialising cache items for a given cache key
68    * 
69    * @param cacheKey
70    */
71   public void initCache(String cacheKey)
72   {
73     String separatedStr = Cache.getProperty(cacheKey);
74     if (separatedStr == null || separatedStr.isEmpty())
75     {
76       return;
77     }
78
79     List<String> persistedCacheItems = Arrays.asList(separatedStr.split(CACHE_DELIMITER));
80
81     LinkedHashSet<String> foundCacheItems = cacheItems.get(cacheKey);
82     if (foundCacheItems == null)
83     {
84       foundCacheItems = new LinkedHashSet<String>();
85     }
86
87     for (String cacheItem : persistedCacheItems)
88     {
89       foundCacheItems.add(cacheItem);
90     }
91     cacheItems.put(cacheKey, foundCacheItems);
92   }
93
94   /**
95    * Method for persisting cache items for a given cache key
96    * 
97    * @param cacheKey
98    */
99   public void persistCache(String cacheKey)
100   {
101     LinkedHashSet<String> foundCacheItems = getAllCachedItemsFor(cacheKey);
102     StringBuffer delimitedCacheBuf = new StringBuffer();
103     for (String cacheItem : foundCacheItems)
104     {
105       delimitedCacheBuf.append(CACHE_DELIMITER).append(cacheItem);
106     }
107     if (delimitedCacheBuf.length() > 0)
108     {
109       delimitedCacheBuf.deleteCharAt(0);
110     }
111     String delimitedCacheString = delimitedCacheBuf.toString();
112
113     Cache.setProperty(cacheKey, delimitedCacheString);
114   }
115
116   public void deleteCacheItems(String cacheKey)
117   {
118     cacheItems.put(cacheKey, new LinkedHashSet<String>());
119     persistCache(cacheKey);
120   }
121
122   public String getCacheLmit(String cacheKey)
123   {
124     String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
125     return Cache.getDefault(uniqueKey, DEFAULT_LIMIT);
126   }
127
128   public int updateCacheLimit(String cacheKey, String newLimit)
129   {
130     String uniqueKey = cacheKey + DEFAULT_LIMIT_KEY;
131     String formerLimit = getCacheLmit(cacheKey);
132     if (newLimit != null && !newLimit.isEmpty()
133             && !formerLimit.equals(newLimit))
134     {
135       Cache.setProperty(uniqueKey, newLimit);
136       formerLimit = newLimit;
137     }
138     return Integer.valueOf(formerLimit);
139   }
140
141   /**
142    * Method for inserting cache items for given cache key into the cache data
143    * structure
144    * 
145    * @param cacheKey
146    * @param cacheItems
147    */
148   public void putCache(String cacheKey, LinkedHashSet<String> cacheItems)
149   {
150     getCacheItems().put(cacheKey, cacheItems);
151   }
152
153   /**
154    * Getter method for obtaining cache data structure
155    * 
156    * @return
157    */
158   private Hashtable<String, LinkedHashSet<String>> getCacheItems()
159   {
160     return cacheItems;
161   }
162 }