-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
188 lines (171 loc) · 4.2 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package cachita
import (
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"reflect"
"strings"
"time"
)
type (
Cache interface {
Get(key string, i interface{}) error
Put(key string, i interface{}, ttl time.Duration) error // ttl 0:default ttl, -1: keep forever
Incr(key string, ttl time.Duration) (int64, error)
Tag(key string, tags ...string) error
Exists(key string) bool
Invalidate(key string) error
InvalidateMulti(keys ...string) error
InvalidateTags(tags ...string) error
}
record struct {
Data interface{}
ExpiredAt time.Time
}
)
var (
ErrNotFound = errors.New("cachita: cache not found")
ErrExpired = errors.New("cachita: cache expired")
)
func calculateTtl(ttl, defaultTtl time.Duration) time.Duration {
if ttl == 0 {
return defaultTtl
} else if ttl == -1 {
return 100000 * time.Hour // 11 years
}
return ttl
}
func expiredAt(ttl, defaultTtl time.Duration) time.Time {
return time.Now().Add(calculateTtl(ttl, defaultTtl))
}
func IsErrorOk(err error) bool {
return err == ErrNotFound || err == ErrExpired
}
func Id(params ...string) string {
s := strings.Join(params, "_")
hash := md5.Sum([]byte(s))
return hex.EncodeToString(hash[:])
}
func runEvery(ttl time.Duration, f func()) {
ticker := time.NewTicker(ttl)
go func() {
for {
select {
case <-ticker.C:
f()
}
}
}()
}
func inArr(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}
func TypeAssert(source, target interface{}) (err error) {
if source == nil {
return nil
}
defer func() {
if r := recover(); r != nil {
var ok bool
err, ok = r.(error)
if !ok {
err = fmt.Errorf("cachita: %v", r)
}
}
}()
if directTypeAssert(source, target) {
return nil
}
v := reflect.ValueOf(target)
if !v.IsValid() {
return errors.New("cachita: target is nil")
}
if v.Kind() != reflect.Ptr {
return fmt.Errorf("cachita: target is not a settable %T", target)
}
v = v.Elem()
if !v.IsValid() {
return fmt.Errorf("cachita: target is not a settable %T", target)
}
s := reflect.ValueOf(source)
if !s.IsValid() {
return errors.New("cachita: source is not valid")
}
s = deReference(s)
v.Set(s)
return nil
}
func directTypeAssert(source, target interface{}) bool {
var ok bool
switch v := target.(type) {
case *string:
*v, ok = source.(string)
case *[]byte:
*v, ok = source.([]byte)
case *int:
*v, ok = source.(int)
case *int8:
*v, ok = source.(int8)
case *int16:
*v, ok = source.(int16)
case *int32:
*v, ok = source.(int32)
case *int64:
*v, ok = source.(int64)
case *uint:
*v, ok = source.(uint)
case *uint8:
*v, ok = source.(uint8)
case *uint16:
*v, ok = source.(uint16)
case *uint32:
*v, ok = source.(uint32)
case *uint64:
*v, ok = source.(uint64)
case *bool:
*v, ok = source.(bool)
case *float32:
*v, ok = source.(float32)
case *float64:
*v, ok = source.(float64)
case *time.Duration:
*v, ok = source.(time.Duration)
case *time.Time:
*v, ok = source.(time.Time)
case *[]string:
*v, ok = source.([]string)
case *map[string]string:
*v, ok = source.(map[string]string)
case *map[string]interface{}:
*v, ok = source.(map[string]interface{})
}
return ok
}
func deReference(v reflect.Value) reflect.Value {
if (v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface) && !v.IsNil() {
return v.Elem()
}
return v
}
func uniqueTags(a []string) (u []string) {
hs := make(map[string]struct{})
for _, s := range a {
if s == "" {
continue
}
hs[s] = struct{}{}
}
for s := range hs {
if s == "" {
continue
}
u = append(u, s)
}
return
}