-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.go
121 lines (102 loc) · 2.75 KB
/
item.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package caching
import (
"bytes"
"encoding/gob"
"time"
"github.com/golang-plus/errors"
)
// Item represents a cache item.
type Item struct {
Key string
Value interface{}
CreatedAt time.Time
AccessedAt time.Time
AbsoluteExpirationTime time.Time
SlidingExpirationPeriod time.Duration
Dependencies []Dependency
}
// NewItem returns a new item.
func NewItem(key string, value interface{}) (*Item, error) {
if len(key) == 0 {
return nil, errors.New("key cannot be empty")
}
return &Item{
Key: key,
Value: value,
CreatedAt: time.Now(),
}, nil
}
// MustNewItem is like as NewItem but panic if key is empty.
func MustNewItem(key string, value interface{}) *Item {
item, err := NewItem(key, value)
if err != nil {
panic(err)
}
return item
}
// touch updates the last accessed timestamp.
func (i *Item) touch() {
i.AccessedAt = time.Now()
}
// SetAbsoluteExpiration sets the Absolute expiration for item.
func (i *Item) SetAbsoluteExpiration(Absolute time.Time) {
i.AbsoluteExpirationTime = Absolute
}
// SetSlidingExpiration sets the Sliding expiration for item.
func (i *Item) SetSlidingExpiration(Sliding time.Duration) {
i.SlidingExpirationPeriod = Sliding
}
// SetDependencies sets the Dependencies for item.
func (i *Item) SetDependencies(Dependencies ...Dependency) {
i.Dependencies = Dependencies
}
// HasExpired reports whether the item has expired.
func (i *Item) HasExpired() bool {
if !i.AbsoluteExpirationTime.IsZero() { // check absolute expiration time
if i.AbsoluteExpirationTime.Before(time.Now()) {
return true
}
}
if i.SlidingExpirationPeriod > 0 { // check sliding expiration period
if !i.AccessedAt.IsZero() { // accessed
if i.AccessedAt.Add(i.SlidingExpirationPeriod).Before(time.Now()) {
return true
}
} else { // never accessed
if i.CreatedAt.Add(i.SlidingExpirationPeriod).Before(time.Now()) {
return true
}
}
}
// check dependencies
for _, Dependency := range i.Dependencies {
if Dependency.HasChanged() {
return true
}
}
return false
}
// Marshal marshals the item to byte data by gob.
func (i *Item) Marshal() ([]byte, error) {
var buffer bytes.Buffer
encoder := gob.NewEncoder(&buffer)
err := encoder.Encode(i)
if err != nil {
return nil, errors.Wrap(err, "could not marshal item to Gob data")
}
return buffer.Bytes(), nil
}
// Unmarshal unmarshals the item from byte data by gob.
func (i *Item) Unmarshal(data []byte) error {
decoder := gob.NewDecoder(bytes.NewBuffer(data))
err := decoder.Decode(i)
if err != nil {
return errors.Wrap(err, "could not unmarshal item from Gob data")
}
return nil
}
// registers types for gob encoding.
func init() {
gob.Register(time.Time{})
gob.Register(Item{})
}