/
types.go
58 lines (50 loc) · 1.23 KB
/
types.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
package failoverassistant
import (
"sync"
"time"
)
// VolumeFailoverRestRequest struct
type VolumeFailoverRestRequest struct {
VolumeName string `json:"volumeName,omitempty"`
StorageClassName string `json:"storageClassName,omitempty"`
VolmeAttachmentID string `json:"volumeAttachmentId,omitempty"`
NodeName string `json:"nodeName,omitempty"`
}
// Cache interface
type Cache interface {
Set(key string, obj interface{}, expiration time.Duration)
Get(key string) (interface{}, bool)
}
type cacheValue struct {
Expire time.Time
Object interface{}
}
type expiredCache struct {
m sync.Map
}
// NewCache creates a cache with expired function
func NewCache() Cache {
return &expiredCache{}
}
func (ec *expiredCache) Set(key string, obj interface{}, expiration time.Duration) {
value := &cacheValue{
Expire: time.Now().Add(expiration),
Object: obj,
}
ec.m.Store(key, value)
}
// return: value, expired
func (ec *expiredCache) Get(key string) (interface{}, bool) {
item, ok := ec.m.Load(key)
if !ok || item == nil {
return nil, true
}
value, ok := item.(*cacheValue)
if !ok {
return nil, true
}
if value.Expire.Before(time.Now()) {
ec.m.Delete(key)
}
return value.Object, value.Expire.Before(time.Now())
}