@SuppressWarnings("unchecked") public void cleanup() { long now = System.currentTimeMillis(); ArrayList<K> keysToDelete = null; synchronized (cacheMap) { MapIterator itr = cacheMap.mapIterator(); keysToDelete = new ArrayList<K>((cacheMap.size() / 2) + 1); K key = null; CachedObject c = null; while (itr.hasNext()) { key = (K) itr.next(); c = (CachedObject) itr.getValue(); if (c != null && (now > ((1000L * timeToLiveInSeconds) + c.lastAccessed))) { keysToDelete.add(key); } } } for (K key : keysToDelete) { synchronized (cacheMap) { cacheMap.remove(key); } Thread.yield(); } }
/** {@inheritDoc} */ @Override public int size() { synchronized (cacheMap) { return cacheMap.size(); } }