public int removeExpired(long expirationAge) {
int expired = 0;
long expirationTime = System.currentTimeMillis() - expirationAge;
for (int i = 0; i < CONCURRENCY_LEVEL; i++) {
RWLock lock = locks[i];
if (!lock.lockWrite(lockWaitTime)) {
log.debug("Could not lock segment " + i + " for cleanup");
continue;
}
try {
for (int j = i; j < capacity; j += CONCURRENCY_LEVEL) {
long currentPtr = mapBase + (long) j * 8;
long nextEntry;
for (long entry = unsafe.getAddress(currentPtr); entry != 0; entry = nextEntry) {
nextEntry = unsafe.getAddress(entry + NEXT_OFFSET);
if (unsafe.getLong(entry + TIME_OFFSET) <= expirationTime) {
unsafe.putAddress(currentPtr, nextEntry);
evicted(entry);
destroyEntry(entry);
expired++;
} else {
currentPtr = entry + NEXT_OFFSET;
}
}
}
} finally {
lock.unlockWrite();
}
}
count.addAndGet(-expired);
expirations.addAndGet(expired);