/
default_adapter.go
172 lines (146 loc) · 3.83 KB
/
default_adapter.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
package persist
import (
"errors"
"fmt"
"github.com/weloe/token-go/persist/cache"
"github.com/weloe/token-go/util"
"log"
"reflect"
"strings"
)
type DefaultAdapter struct {
cache cache.Cache
serializer Serializer
enableRefreshTimer bool
}
var _ Adapter = (*DefaultAdapter)(nil)
func NewDefaultAdapter() *DefaultAdapter {
return &DefaultAdapter{
cache: cache.NewDefaultLocalCache(),
enableRefreshTimer: true,
}
}
func (d *DefaultAdapter) SetCache(cache cache.Cache) {
d.cache = cache
}
func (d *DefaultAdapter) SetSerializer(serializer Serializer) {
d.serializer = serializer
}
// GetStr if key is expired delete it before get data
func (d *DefaultAdapter) GetStr(key string) string {
value := d.cache.Get(key)
if value == nil {
return ""
}
return fmt.Sprintf("%v", value)
}
func (d *DefaultAdapter) SetStr(key string, value string, timeout int64) error {
return d.cache.Set(key, value, timeout)
}
func (d *DefaultAdapter) UpdateStr(key string, value string) error {
return d.cache.Update(key, value)
}
func (d *DefaultAdapter) DeleteStr(key string) error {
return d.cache.Delete(key)
}
func (d *DefaultAdapter) GetStrTimeout(key string) int64 {
return d.cache.GetTimeout(key)
}
func (d *DefaultAdapter) UpdateStrTimeout(key string, timeout int64) error {
return d.cache.UpdateTimeout(key, timeout)
}
// interface{} operation
//
//
func (d *DefaultAdapter) Get(key string, t ...reflect.Type) interface{} {
value := d.cache.Get(key)
if d.serializer == nil || t == nil || len(t) == 0 {
return value
}
bytes, err := util.InterfaceToBytes(value)
if err != nil {
log.Printf("Adapter.Get() failed: %v", err)
return nil
}
instance := reflect.New(t[0].Elem()).Interface()
err = d.serializer.UnSerialize(bytes, instance)
if err != nil {
log.Printf("Adapter.Get() failed: %v", err)
return nil
}
return instance
}
func (d *DefaultAdapter) Set(key string, value interface{}, timeout int64) error {
if d.serializer == nil {
return d.cache.Set(key, value, timeout)
}
bytes, err := d.serializer.Serialize(value)
if err != nil {
return err
}
return d.cache.Set(key, bytes, timeout)
}
func (d *DefaultAdapter) Update(key string, value interface{}) error {
if d.serializer == nil {
return d.cache.Update(key, value)
}
bytes, err := d.serializer.Serialize(value)
if err != nil {
return err
}
return d.cache.Update(key, bytes)
}
func (d *DefaultAdapter) GetTimeout(key string) int64 {
return d.cache.GetTimeout(key)
}
func (d *DefaultAdapter) UpdateTimeout(key string, timeout int64) error {
return d.cache.UpdateTimeout(key, timeout)
}
func (d *DefaultAdapter) Delete(key string) error {
return d.cache.Delete(key)
}
func (d *DefaultAdapter) DeleteBatchFilteredKey(keyPrefix string) error {
var err error
cacheEx, ok := d.cache.(cache.CacheEx)
if !ok {
return errors.New("the cache does not implement the Range method")
}
cacheEx.Range(func(key, value any) bool {
if strings.HasPrefix(key.(string), keyPrefix) {
err = d.cache.Delete(key.(string))
if err != nil {
return false
}
}
return true
})
return err
}
func (d *DefaultAdapter) GetCountsFilteredKey(keyPrefix string) (int, error) {
cacheEx, ok := d.cache.(cache.CacheEx)
if !ok {
return 0, errors.New("the cache does not implement the Range method")
}
var counts int
cacheEx.Range(func(key, value any) bool {
if strings.HasPrefix(key.(string), keyPrefix) {
counts++
}
return true
})
return counts, nil
}
func (d *DefaultAdapter) EnableCleanTimer(b bool) {
d.enableRefreshTimer = b
}
func (d *DefaultAdapter) GetCleanTimer() bool {
return d.enableRefreshTimer
}
func (d *DefaultAdapter) StartCleanTimer(period int64) error {
cacheEx, ok := d.cache.(cache.CacheEx)
if !ok {
return errors.New("the Cache does not implement the StartCleanTimer method")
}
cacheEx.EnableCleanTimer(period)
return nil
}