-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.go
67 lines (58 loc) · 1.32 KB
/
entry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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,
}
}