-
Notifications
You must be signed in to change notification settings - Fork 4
/
item.go
65 lines (55 loc) · 979 Bytes
/
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
package cacher
import (
"time"
)
type (
// Item :nodoc:
Item interface {
GetTTLInt64() int64
GetKey() string
GetValue() any
SetTTL(ttl time.Duration)
}
item struct {
key string
value any
ttl time.Duration
}
)
// WithTTL define custom TTL used in GetOrSet
func WithTTL(ttl time.Duration) func(Item) {
return func(i Item) {
i.SetTTL(ttl)
}
}
// NewItem :nodoc:
func NewItem(key string, value any) Item {
return &item{
key: key,
value: value,
}
}
// NewItemWithCustomTTL :nodoc:
func NewItemWithCustomTTL(key string, value any, customTTL time.Duration) Item {
return &item{
key: key,
value: value,
ttl: customTTL,
}
}
// GetTTLInt64 :nodoc:
func (i *item) GetTTLInt64() int64 {
return int64(i.ttl.Seconds())
}
// SetTTL set TTL
func (i *item) SetTTL(ttl time.Duration) {
i.ttl = ttl
}
// GetKey :nodoc:
func (i *item) GetKey() string {
return i.key
}
// GetValue :nodoc:
func (i *item) GetValue() any {
return i.value
}