-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
156 lines (138 loc) · 3.1 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
package cache
import (
"encoding/json"
"errors"
"fmt"
"time"
"github.com/cachego/cache"
)
// NewInDiskStrCache returns a new in-disk string cache
// with the given cachePath. eg: ".cache", "~/.cache", "/tmp/cache"
func NewInDiskStrCache(cachePath string) cache.Cache {
return &inDiskStrCache{
CachePath: cachePath,
KeyPath: fmt.Sprintf("%s/keys", cachePath),
}
}
type inDiskStrCache struct {
CachePath string // cache file path
KeyPath string // cache file path
}
type inDiskStrCacheItem struct {
Val string
Exp int64
}
func (i *inDiskStrCacheItem) JsonEncode() ([]byte, error) {
return json.Marshal(i)
}
func (i *inDiskStrCacheItem) JsonDecode(jsonStr string) error {
return json.Unmarshal([]byte(jsonStr), i)
}
type cacheKey struct {
KMap map[string]bool
}
func (c *cacheKey) JsonEncode() ([]byte, error) {
return json.Marshal(c)
}
func (c *cacheKey) JsonDecode(jsonStr string) error {
return json.Unmarshal([]byte(jsonStr), c)
}
func (c *inDiskStrCache) Get(key string) (val interface{}, err error) {
v, err := GetFileData(c.CachePath, key)
if err != nil || v == nil {
return nil, nil
}
item := inDiskStrCacheItem{}
err = item.JsonDecode(string(v))
if err != nil {
return
}
if item.Exp != 0 && item.Exp < time.Now().Unix() {
c.Del(key)
val = nil
} else {
val = item.Val
}
return
}
func (c *inDiskStrCache) Del(key string) (err error) {
return DeleteFile(c.CachePath, key)
}
func (c *inDiskStrCache) Set(key string, val interface{}, ttl time.Duration) error {
if v, ok := val.(string); ok {
exp := int64(0)
if ttl != 0 {
exp = time.Now().Add(ttl).Unix()
}
cacheItem := inDiskStrCacheItem{
Val: v,
Exp: exp,
}
err := c.saveKey(key)
if err != nil {
return err
}
data, err := cacheItem.JsonEncode()
if err != nil {
return err
}
return CoverFile(c.CachePath, key, data)
} else {
return errors.New("inDiskStrCache just support string values")
}
}
func (c *inDiskStrCache) IsHit(key string) (isHit bool, err error) {
val, err := c.Get(key)
if err != nil {
return
}
return val != nil, nil
}
func (c *inDiskStrCache) Clear() (err error) {
keyMaps, err := c.getAllKeyMaps()
if err != nil || keyMaps.KMap == nil {
return nil
}
for key, _ := range keyMaps.KMap {
if _, err = c.Get(key); err != nil {
return
}
}
return c.deleteKeyMap()
}
func (c *inDiskStrCache) saveKey(key string) error {
v, _ := GetFileData(c.KeyPath, "keyMap")
keyMaps := cacheKey{}
if v == nil {
keyMaps.KMap = make(map[string]bool)
} else {
err := keyMaps.JsonDecode(string(v))
if err != nil {
return err
}
if keyMaps.KMap == nil {
keyMaps.KMap = make(map[string]bool)
}
}
keyMaps.KMap[key] = true
data, err := keyMaps.JsonEncode()
if err != nil {
return err
}
return CoverFile(c.KeyPath, "keyMap", data)
}
func (c *inDiskStrCache) getAllKeyMaps() (keys cacheKey, err error) {
v, _ := GetFileData(c.KeyPath, "keyMap")
if v == nil {
keys.KMap = make(map[string]bool)
} else {
err = keys.JsonDecode(string(v))
if err != nil {
return
}
}
return
}
func (c *inDiskStrCache) deleteKeyMap() error {
return DeleteFile(c.KeyPath, "keyMap")
}