-
Notifications
You must be signed in to change notification settings - Fork 402
/
cache.go
116 lines (99 loc) · 2.91 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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package trust
import (
"context"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"github.com/zeebo/errs"
"storj.io/common/fpath"
)
// Cache caches source information about trusted satellites.
type Cache struct {
path string
data *CacheData
}
// LoadCache loads a cache from a file on disk. If the file is not present, the
// cache is still loaded. If the file cannot be read for any other reason, the
// function will return an error. LoadCache ensures the containing directory
// exists.
func LoadCache(path string) (*Cache, error) {
if path == "" {
return nil, Error.New("cache path cannot be empty")
}
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
return nil, Error.New("unable to make cache parent directory: %w", err)
}
data, err := LoadCacheData(path)
switch {
case err == nil:
case errs.IsFunc(err, os.IsNotExist):
data = NewCacheData()
default:
return nil, err
}
return &Cache{
path: path,
data: data,
}, nil
}
// Path returns the path on disk to the file containing the cache.
func (cache *Cache) Path() string {
return cache.path
}
// Lookup takes a cache key and returns entries associated with that key. If
// the key is unset in the cache, false is returned for ok. Otherwise the
// entries are returned with ok returned as true.
func (cache *Cache) Lookup(key string) (entries []Entry, ok bool) {
entries, ok = cache.data.Entries[key]
return entries, ok
}
// Set sets the entries in the cache for the provided key.
func (cache *Cache) Set(key string, entries []Entry) {
cache.data.Entries[key] = entries
}
// Save persists the cache to disk.
func (cache *Cache) Save(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
return SaveCacheData(cache.path, cache.data)
}
// CacheData represents the data stored in the cache.
type CacheData struct {
Entries map[string][]Entry `json:"entries"`
}
// NewCacheData returns an new CacheData.
func NewCacheData() *CacheData {
return &CacheData{
Entries: make(map[string][]Entry),
}
}
// LoadCacheData loads the cache data from the given path.
func LoadCacheData(path string) (*CacheData, error) {
dataBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, Error.Wrap(err)
}
data := NewCacheData()
if err := json.Unmarshal(dataBytes, data); err != nil {
return nil, Error.New("malformed cache: %w", err)
}
// Ensure the entries map is always non-nil on load
if data.Entries == nil {
data.Entries = map[string][]Entry{}
}
return data, nil
}
// SaveCacheData persists the cache data to the given path.
func SaveCacheData(path string, data *CacheData) error {
// Ensure the entries map is always non-nil on save
if data.Entries == nil {
data.Entries = map[string][]Entry{}
}
dataBytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
return Error.Wrap(err)
}
return fpath.AtomicWriteFile(path, dataBytes, 0644)
}