All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.greencheek.caching.herdcache.lru.expiry.IdleTimedEntryWithExpiry Maven / Gradle / Ivy

Go to download

A cache that uses futures to prevent thundering herds to your backend service

There is a newer version: 2.0.19
Show newest version
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