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

org.rx.core.CachePolicy Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.core;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.rx.bean.DateTime;

import java.io.Serializable;

@AllArgsConstructor
public class CachePolicy implements Serializable {
    private static final long serialVersionUID = 4378825072232415879L;

    public static CachePolicy today() {
        return today(Constants.ONE_DAY_TOTAL_SECONDS);
    }

    public static CachePolicy today(int expireSeconds) {
        DateTime now = DateTime.now(), expire = now.addSeconds(expireSeconds);
        DateTime max = now.setTimePart("23:59:59");
        return new CachePolicy((expire.before(max) ? expire : max).getTime(), 0);
    }

    public static CachePolicy absolute(int expireSeconds) {
        return new CachePolicy(DateTime.now().addSeconds(expireSeconds).getTime(), 0);
    }

    public static CachePolicy sliding(int expireSeconds) {
        return new CachePolicy(DateTime.now().addSeconds(expireSeconds).getTime(), expireSeconds * 1000);
    }

    @Getter
    long expiration = Long.MAX_VALUE;
    int slidingSpan;

    public boolean isExpired() {
        return expiration <= System.currentTimeMillis();
    }

    protected CachePolicy(CachePolicy policy) {
        if (policy == null) {
            return;
        }
        this.expiration = policy.expiration;
        this.slidingSpan = policy.slidingSpan;
    }

    public long ttl() {
        return ttl(slidingSpan > 0);
    }

    public long ttl(boolean slidingRenew) {
        long ttl = Math.max(0, expiration - System.currentTimeMillis());
        if (ttl > 0 && slidingRenew) {
            Tasks.setTimeout(this::slidingRenew, 100, this, Constants.TIMER_REPLACE_FLAG);
//            slidingRenew();
        }
        return ttl;
    }

    public boolean slidingRenew() {
        if (slidingSpan <= 0) {
            return false;
        }

        if ((expiration += slidingSpan) < 0) {
            expiration = Long.MAX_VALUE;
        }
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy