public static void removeAllWithStringPrefix(AbstractCache<String, ?> cache, String urlPrefix) { Set<String> keys = cache.keySet(); for (String key : keys) { if (key.startsWith(urlPrefix)) { cache.remove(key); } } if (cache.isDiskCacheEnabled()) { removeExpiredCache(cache, urlPrefix); } }
/** * Add buildings to cache. * * @param buildingsFromKulturarv buildings to cache */ public void add(List<Building> buildingsFromKulturarv) { Map<Long, Building> map = new HashMap<Long, Building>(); for (Building building : buildingsFromKulturarv) { map.put(building.getId(), building); } super.add(map); }
private static void removeExpiredCache( final AbstractCache<String, ?> cache, final String urlPrefix) { final File cacheDir = new File(cache.getDiskCacheDirectory()); if (!cacheDir.exists()) { return; } File[] list = cacheDir.listFiles( new FilenameFilter() { @Override public boolean accept(File dir, String filename) { return dir.equals(cacheDir) && filename.startsWith(cache.getFileNameForKey(urlPrefix)); } }); if (list == null || list.length == 0) { return; } for (File file : list) { file.delete(); } }
@Override @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE") public final VALUETYPE getFromCache(final KEYTYPE aKey) { // read existing value VALUETYPE aValue = super.getFromCacheNoStats(aKey); if (aValue == null) { // No old value in the cache m_aRWLock.writeLock().lock(); try { // Read again, in case the value was set between the two locking // sections // Note: do not increase statistics in this second try aValue = super.getFromCacheNoStatsNotLocked(aKey); if (aValue == null) { // Call the abstract method to create the value to cache aValue = getValueToCache(aKey); // Just a consistency check if (aValue == null) throw new IllegalStateException("The value to cache was null for key '" + aKey + "'"); // Put the new value into the cache super.putInCacheNotLocked(aKey, aValue); m_aCacheAccessStats.cacheMiss(); } else m_aCacheAccessStats.cacheHit(); } finally { m_aRWLock.writeLock().unlock(); } } else m_aCacheAccessStats.cacheHit(); return aValue; }
@Override void destroy() { // cache.dispose(); be careful, it must be done by cacheManager cacheManager.cachesByName.remove(getName()); super.destroy(); }