001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.components.cache.caffeine;
017
018
019import org.apache.commons.lang3.time.DateUtils;
020
021import java.io.Serializable;
022import java.util.Date;
023
024public class CaffeineCacheObject implements Serializable {
025
026
027    private Object value;
028    private Integer liveSeconds;
029    private Long cachetime;
030
031    public CaffeineCacheObject() {
032    }
033
034    public CaffeineCacheObject(Object value) {
035        this.value = value;
036    }
037
038    public CaffeineCacheObject(Object value, Integer liveSeconds) {
039        this.value = value;
040        this.liveSeconds = liveSeconds;
041    }
042
043    public Object getValue() {
044        return value;
045    }
046
047    public void setValue(Object value) {
048        this.value = value;
049    }
050
051    public Integer getTtl() {
052        if (liveSeconds == null) {
053            return -1;
054        }
055
056        long timeMillis = cachetime - DateUtils.addSeconds(new Date(), -liveSeconds).getTime();
057
058        if (timeMillis > 0) {
059            return (int) (timeMillis / 1000);
060        }
061
062        return 0;
063    }
064
065    public Integer getLiveSeconds() {
066        return liveSeconds;
067    }
068
069    public void setLiveSeconds(Integer liveSeconds) {
070        this.liveSeconds = liveSeconds;
071    }
072
073    public Long getCachetime() {
074        return cachetime;
075    }
076
077    public void setCachetime(Long cachetime) {
078        this.cachetime = cachetime;
079    }
080
081    public boolean isDue() {
082        if (liveSeconds == null) {
083            return false;
084        }
085
086        if (DateUtils.addSeconds(new Date(), -liveSeconds).getTime() > cachetime) {
087            return true;
088        }
089
090        return false;
091    }
092}