org.greencheek.caching.herdcache.lru.expiry.IdleTimedEntryWithExpiry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of herdcache Show documentation
Show all versions of herdcache Show documentation
A cache that uses futures to prevent thundering herds to your backend service
package org.greencheek.caching.herdcache.lru.expiry;
import com.google.common.util.concurrent.SettableFuture;
/**
* Created by dominictootell on 28/07/2014.
*/
public class IdleTimedEntryWithExpiry implements TimedEntry {
private final SettableFuture future;
private volatile long createdAt;
private volatile long lastAccessed;
public IdleTimedEntryWithExpiry(SettableFuture promise) {
future = promise;
createdAt = System.nanoTime();
lastAccessed = System.nanoTime();
}
@Override
public SettableFuture getFuture() {
return future;
}
@Override
public boolean hasNotExpired(ExpiryTimes expiryTimes) {
long now = System.nanoTime();
return (
(createdAt + expiryTimes.getTimeToLiveInNanos() ) > now &&
(lastAccessed + expiryTimes.getTimeToIdleInNanos()) > now
);
}
@Override
public long getCreatedAt() {
return createdAt;
}
@Override
public void setCreatedAt(long createdAt) {
this.createdAt = createdAt;
lastAccessed = System.nanoTime();
}
@Override
public void touch() {
lastAccessed = System.nanoTime();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy