/
local_cache.go
48 lines (43 loc) · 955 Bytes
/
local_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
package dbcache
// 本地缓存
import (
"github.com/patrickmn/go-cache"
"time"
)
var localCacheHandler *cache.Cache
var _ Cache = (*localCache)(nil)
type localCache struct {
}
func NewLocalCache() *localCache {
if localCacheHandler == nil {
localCacheHandler = cache.New(5*time.Minute, 10*time.Minute)
}
return &localCache{}
}
func (r *localCache) Set(key, value string, expire ...int) bool {
if len(expire) > 0 {
localCacheHandler.Set(key, value, time.Duration(expire[0])*time.Second)
} else {
localCacheHandler.Set(key, value, cache.NoExpiration)
}
return true
}
func (r *localCache) Get(key string) string {
res, ok := localCacheHandler.Get(key)
if !ok {
return ""
}
return res.(string)
}
func (r *localCache) Del(key string) int {
localCacheHandler.Delete(key)
return 1
}
func (r *localCache) Expire(key string, expire int) int {
value := r.Get(key)
if value == "" {
return 0
}
r.Set(key, value, expire)
return 1
}