Skip to content

Commit

Permalink
src: re-arrange code to have constructors before methods
Browse files Browse the repository at this point in the history
Acked-by: Paul Millar
Target: master
  • Loading branch information
kofemann committed Nov 6, 2018
1 parent cc674c2 commit 254ee60
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions core/src/main/java/org/dcache/utils/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,36 +57,6 @@ public class Cache<K, V> {
private static final Logger _log = LoggerFactory.getLogger(Cache.class);
private final Clock _timeSource;

/**
* Check and remove expired entries.
*/
public void cleanUp() {
List<V> expiredEntries = new ArrayList<>();

_accessLock.lock();
try {
long now = System.currentTimeMillis();
Iterator<Map.Entry<K, CacheElement<V>>> entries = _storage.entrySet().iterator();
while(entries.hasNext()) {
Map.Entry<K, CacheElement<V>> entry = entries.next();
CacheElement<V> cacheElement = entry.getValue();

if (!cacheElement.validAt(now)) {
_log.debug("Cleaning expired entry key = [{}], value = [{}]",
entry.getKey(), cacheElement.getObject());
entries.remove();
expiredEntries.add(cacheElement.getObject());
}
}
_lastClean.set(now);
} finally {
_accessLock.unlock();
}
for (V v : expiredEntries) {
_eventListener.notifyExpired(this, v);
}
}

/**
* The name of this cache.
*/
Expand Down Expand Up @@ -354,6 +324,35 @@ public void clear() {
}
}

/**
* Check and remove expired entries.
*/
public void cleanUp() {
List<V> expiredEntries = new ArrayList<>();

_accessLock.lock();
try {
long now = System.currentTimeMillis();
Iterator<Map.Entry<K, CacheElement<V>>> entries = _storage.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<K, CacheElement<V>> entry = entries.next();
CacheElement<V> cacheElement = entry.getValue();

if (!cacheElement.validAt(now)) {
_log.debug("Cleaning expired entry key = [{}], value = [{}]",
entry.getKey(), cacheElement.getObject());
entries.remove();
expiredEntries.add(cacheElement.getObject());
}
}
_lastClean.set(now);
} finally {
_accessLock.unlock();
}

expiredEntries.forEach( v -> _eventListener.notifyExpired(this, v));
}

/**
* Get {@link List<V>} of entries.
* @return list of entries.
Expand Down

0 comments on commit 254ee60

Please sign in to comment.