-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_cache.go
52 lines (41 loc) · 906 Bytes
/
file_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
package braintree
import (
"encoding/json"
"os"
braintreeAPI "github.com/lionelbarrow/braintree-go"
)
type FileCache struct {
filename string
}
func NewFileCache(filename string) *FileCache {
return &FileCache{
filename: filename,
}
}
func (self *FileCache) Clear() {
os.Remove(self.filename)
}
func (self *FileCache) Entries() ([]*braintreeAPI.Plan, error) {
result := []*braintreeAPI.Plan{}
in, err := os.Open(self.filename)
if err != nil {
return nil, ErrCacheMiss
}
dec := json.NewDecoder(in)
if err := dec.Decode(&result); err != nil {
return nil, ErrCacheMiss
}
return result, nil
}
func (self *FileCache) Set(entries []*braintreeAPI.Plan) error {
out, err := os.Create(self.filename)
if err != nil {
return ErrCacheInternal
}
defer out.Close()
enc := json.NewEncoder(out)
if err := enc.Encode(entries); err != nil {
return ErrCacheInternal
}
return nil
}