13 Star 89 Fork 13

kelvins-io/g2cache

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
entry.go 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
cristiane 提交于 2021-06-25 09:22 +08:00 . !3发布订阅fix
package g2cache
import (
jsoniter "github.com/json-iterator/go"
"time"
)
var (
EntryLazyFactor = 32
)
type Entry struct {
Value interface{} `json:"value"`
TtlSecond int `json:"ttl"`
Obsolete int64 `json:"obsolete"`
Expiration int64 `json:"expiration"`
}
// Outdated data means that the data is still available, but not up-to-date
func (e *Entry) Obsoleted() bool {
if e.Obsolete <= 0 {
return true
}
if e.Obsolete < time.Now().Unix() {
return true
}
return false
}
func (e *Entry) GetObsoleteTTL() (second int64) {
return e.Obsolete - time.Now().Unix()
}
// Expired means that the data is unavailable and data needs to be synchronized
func (e *Entry) Expired() bool {
if e.Expiration <= 0 {
return true
}
if e.Expiration < time.Now().Unix() {
return true
}
return false
}
func (e *Entry) GetExpireTTL() (second int64) {
return e.Expiration - time.Now().Unix()
}
func (e *Entry) String() string {
s, _ := jsoniter.MarshalToString(e.Value)
return s
}
func NewEntry(v interface{}, second int) *Entry {
ttl := second
var od, e int64
if second > 0 {
od = time.Now().Add(time.Duration(second) * time.Second).Unix()
e = time.Now().Add(time.Duration(second*EntryLazyFactor) * time.Second).Unix()
}
return &Entry{
Value: v,
TtlSecond: ttl,
Obsolete: od,
Expiration: e,
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kelvins-io/g2cache.git
git@gitee.com:kelvins-io/g2cache.git
kelvins-io
g2cache
g2cache
release

搜索帮助