/
cache.go
126 lines (110 loc) · 2.34 KB
/
cache.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
122
123
124
125
126
package cache
import (
"errors"
"fmt"
"sync"
"time"
"github.com/go-cacher/cacher"
)
/*MapCache MapCache */
type MapCache struct {
cache sync.Map
}
type cacheData struct {
value interface{}
life *time.Time
}
var NotFoundError = errors.New("data not found")
var WrongTypeError = errors.New("cast wrong type error")
/*Get the exist value */
func (m MapCache) Get(key string) ([]byte, error) {
if v, b := m.cache.Load(key); b {
switch vv := v.(type) {
case *cacheData:
if vv.life != nil && vv.life.Before(time.Now()) {
m.cache.Delete(key)
return nil, NotFoundError
}
if v, b := vv.value.([]byte); b {
return v, nil
}
return nil, WrongTypeError
}
}
return nil, NotFoundError
}
//get a value with a default value
func (m MapCache) GetD(key string, v []byte) []byte {
if ret, e := m.Get(key); e == nil {
return ret
}
return v
}
// set a value to cache
func (m *MapCache) Set(key string, val []byte) error {
m.cache.Store(key, &cacheData{
value: val,
life: nil,
})
return nil
}
func (m *MapCache) SetWithTTL(key string, val []byte, ttl int64) error {
t := time.Now().Add(time.Duration(ttl))
m.cache.Store(key, &cacheData{
value: val,
life: &t,
})
return nil
}
func (m MapCache) Has(key string) (bool, error) {
if _, e := m.Get(key); e != nil {
if errors.Is(e, NotFoundError) {
return false, nil
}
return false, e
}
return true, nil
}
func (m *MapCache) Delete(key string) error {
m.cache.Delete(key)
return nil
}
func (m *MapCache) Clear() error {
m.cache = sync.Map{}
return nil
}
/*GetMultiple get multiple values */
func (m MapCache) GetMultiple(keys ...string) (map[string][]byte, error) {
vals := make(map[string][]byte, len(keys))
for _, key := range keys {
if ret, e := m.Get(key); e == nil {
vals[key] = ret
}
return nil, fmt.Errorf("%s:%+v", key, NotFoundError)
}
return vals, nil
}
/*SetMultiple set multiple values */
func (m *MapCache) SetMultiple(values map[string][]byte) error {
for k, v := range values {
e := m.Set(k, v)
if e != nil {
return fmt.Errorf("%s:%+v", k, e)
}
}
return nil
}
/*DeleteMultiple delete multiple values */
func (m *MapCache) DeleteMultiple(keys ...string) error {
for _, key := range keys {
e := m.Delete(key)
if e != nil {
return fmt.Errorf("%s:%+v", key, e)
}
}
return nil
}
// New ...
func New() *MapCache {
return &MapCache{}
}