-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.manager.code.go
62 lines (50 loc) · 1.1 KB
/
store.manager.code.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
package store
import (
"github.com/lishimeng/x/factory"
"github.com/patrickmn/go-cache"
"time"
)
func GetManager() Manager {
var m Manager
_ = factory.Get(&m)
return m
}
type Manager interface {
GetDefaultStore() CodeStore
}
type SimpleStoreManager struct {
defaultStore CodeStore
}
func NewStoreManager() (s Manager) {
ssm := &SimpleStoreManager{}
ssm.defaultStore = NewRamStore()
s = ssm
return
}
func (s *SimpleStoreManager) GetDefaultStore() CodeStore {
return s.defaultStore
}
type Option func()
type CodeStore interface {
Save(code string, payload string, options ...Option)
Load(code string) (string, bool) // payload, found
}
type MapRamCodeStore struct {
c *cache.Cache
}
func (m *MapRamCodeStore) Save(code string, payload string, options ...Option) {
m.c.Set(code, payload, cache.DefaultExpiration)
}
func (m *MapRamCodeStore) Load(code string) (string, bool) {
v, found := m.c.Get(code)
if !found {
return "", false
}
m.c.Delete(code)
return v.(string), true
}
func NewRamStore() (cs CodeStore) {
s := &MapRamCodeStore{c: cache.New(5*time.Minute, 10*time.Minute)}
cs = s
return
}