-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
121 lines (104 loc) · 2.2 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
package requests
import (
"crypto/md5"
"encoding/hex"
"errors"
"io/ioutil"
"net/http"
"net/http/httputil"
"os"
"path"
"path/filepath"
"sync"
)
var ErrNotExist = errors.New("not exist")
type Cache interface {
Hash(*Request) (string, error)
Load(name string) (*Response, error)
Save(name string, resp *Response) error
Del(name string) error
}
func FileCacheDir(s string) fileCacheDir {
return fileCacheDir(s)
}
func MemoryCache() memoryCacheDir {
return memoryCacheDir{}
}
type memoryCacheDir struct {
m sync.Map
}
func (f memoryCacheDir) Hash(r *Request) (string, error) {
req, err := r.RawRequest()
if err != nil {
return "", err
}
return RequestHash(req)
}
func (f memoryCacheDir) Load(name string) (*Response, error) {
d, ok := f.m.Load(name)
if !ok {
return nil, ErrNotExist
}
data, ok := d.(*Response)
if !ok {
return nil, ErrNotExist
}
return data, nil
}
func (f memoryCacheDir) Save(name string, resp *Response) error {
f.m.Store(name, resp)
return nil
}
func (f memoryCacheDir) Del(name string) error {
f.m.Delete(name)
return nil
}
type fileCacheDir string
func (f fileCacheDir) Hash(r *Request) (string, error) {
req, err := r.RawRequest()
if err != nil {
return "", err
}
h, err := RequestHash(req)
if err != nil {
return "", err
}
return path.Join(req.URL.Scheme, req.URL.Host, req.URL.Path, h), nil
}
func (f fileCacheDir) Load(name string) (*Response, error) {
data, err := ioutil.ReadFile(path.Join(string(f), name))
if err != nil {
return nil, ErrNotExist
}
resp := &Response{}
err = resp.UnarshalText(data)
if err != nil {
return nil, err
}
return resp, nil
}
func (f fileCacheDir) Save(name string, resp *Response) error {
data, err := resp.MarshalText()
if err != nil {
return err
}
p := filepath.Join(string(f), name)
dir, _ := filepath.Split(p)
err = os.MkdirAll(dir, 0755)
if err != nil {
return err
}
return ioutil.WriteFile(p, data, 0644)
}
func (f fileCacheDir) Del(name string) error {
os.Remove(path.Join(string(f), name))
return nil
}
func RequestHash(r *http.Request) (string, error) {
msg, err := httputil.DumpRequest(r, true)
if err != nil {
return "", err
}
sum := md5.Sum(msg)
return hex.EncodeToString(sum[:]), nil
}