9 Star 44 Fork 6

kelvins-io / g2cache

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
entry.go 1.39 KB
Copy Edit Web IDE Raw Blame History
雨化田 authored 2021-04-23 11:27 . 必要的安全检查
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().UnixNano() {
return true
}
return false
}
func (e *Entry) GetObsoleteTTL() (second int64) {
return (e.Obsolete - time.Now().UnixNano()) / int64(time.Second)
}
// 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().UnixNano() {
return true
}
return false
}
func (e *Entry) GetExpireTTL() (second int64) {
return (e.Expiration - time.Now().UnixNano()) / int64(time.Second)
}
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).UnixNano()
e = time.Now().Add(time.Duration(second*EntryLazyFactor) * time.Second).UnixNano()
}
return &Entry{
Value: v,
TtlSecond: ttl,
Obsolete: od,
Expiration: e,
}
}

Comment ( 0 )

Sign in for post a comment

Go
1
https://gitee.com/kelvins-io/g2cache.git
git@gitee.com:kelvins-io/g2cache.git
kelvins-io
g2cache
g2cache
release

Search