-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem.go
More file actions
67 lines (59 loc) · 1.26 KB
/
item.go
File metadata and controls
67 lines (59 loc) · 1.26 KB
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 ttlmap
import (
"sync"
"time"
)
// Item represents a record in the map
type Item struct {
sync.RWMutex
updateMutex sync.RWMutex
isUpdating bool
data interface{}
deadline time.Time
ttl time.Duration
expires *time.Time
onDelete func(*Item)
}
func newItem(value interface{}, duration time.Duration, deadline time.Time, onDelete func(*Item)) *Item {
i := &Item{
data: value,
ttl: duration,
deadline: deadline,
onDelete: onDelete,
}
expiry := time.Now().Add(duration)
i.expires = &expiry
return i
}
// Touch increases the expiry time on the item by the TTL
func (i *Item) Touch() {
i.Lock()
expiration := time.Now().Add(i.ttl)
i.expires = &expiration
i.Unlock()
}
// Expired returns if the item has passed its expiry time
func (i *Item) Expired() bool {
var value bool
i.RLock()
if i.expires == nil || i.deadline.Before(time.Now()) {
value = true
} else {
value = i.expires.Before(time.Now())
}
i.RUnlock()
return value
}
// GetValue represents the value of the item in the map
func (i *Item) GetValue() interface{} {
return i.data
}
func (i *Item) GetExpiry() time.Time {
if i.expires == nil {
return i.deadline
}
return *i.expires
}
func (i *Item) GetDeadline() time.Time {
return i.deadline
}