-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
46 lines (38 loc) · 811 Bytes
/
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
package main
import (
"encoding/json"
"errors"
"time"
"github.com/patrickmn/go-cache"
)
type CacheItf interface {
Set(key string, data interface{}, expiration time.Duration) error
Get(key string) ([]byte, error)
}
type AppCache struct {
client *cache.Cache
}
func (r *AppCache) Set(key string, data interface{}, expiration time.Duration) error {
b, err := json.Marshal(data)
if err != nil {
return err
}
r.client.Set(key, b, expiration)
return nil
}
func (r *AppCache) Get(key string) ([]byte, error) {
res, exist := r.client.Get(key)
if !exist {
return nil, nil
}
resByte, ok := res.([]byte)
if !ok {
return nil, errors.New("Format is not arr of bytes")
}
return resByte, nil
}
func InitCache() {
myCache = &AppCache{
client: cache.New(10*time.Minute, 10*time.Minute),
}
}