-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
172 lines (154 loc) · 3.73 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package internal
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"time"
"github.com/Masterminds/semver"
)
const defaultCacheFileName = "modules"
type modulesCache interface {
Load(path string, version *semver.Version) (*Module, bool)
Save(m *Module) error
}
type cachePersistenceLayer interface {
Save(module persistedModule) error
Load() ([]persistedModule, error)
}
func NewCache(filePath string) (*Cache, error) {
persistence, err := newFilePersistence(filePath)
if err != nil {
return nil, err
}
cache := &Cache{
Modules: make(map[string]*Module),
rwm: sync.RWMutex{},
persistence: persistence,
}
return cache, cache.loadFromPersistence()
}
type Cache struct {
Modules map[string]*Module
rwm sync.RWMutex
persistence cachePersistenceLayer
}
func (c *Cache) Load(path string, version *semver.Version) (module *Module, loaded bool) {
c.rwm.RLock()
defer c.rwm.RUnlock()
module, loaded = c.Modules[c.moduleHash(path, version)]
return
}
func (c *Cache) Save(m *Module) error {
if c.Has(m) {
return nil
}
c.rwm.Lock()
defer c.rwm.Unlock()
// Second check if we were racing with another goroutine.
// We can't use read locks now (deadlock).
_, has := c.Modules[c.moduleHash(m.Path, m.Version)]
if has {
return nil
}
c.Modules[c.moduleHash(m.Path, m.Version)] = m
if c.persistence == nil {
return nil
}
return c.persistence.Save(persistedModule{
Path: m.Path,
Version: m.Version,
Time: m.Time,
})
}
func (c *Cache) Has(m *Module) bool {
c.rwm.RLock()
defer c.rwm.RUnlock()
_, has := c.Modules[c.moduleHash(m.Path, m.Version)]
return has
}
type persistedModule struct {
Path string `json:"path"`
Version *semver.Version `json:"version"`
Time time.Time `json:"time"`
}
func (c *Cache) loadFromPersistence() error {
if c.persistence == nil {
return nil
}
c.rwm.Lock()
defer c.rwm.Unlock()
modules, err := c.persistence.Load()
if err != nil {
return err
}
for _, m := range modules {
hash := c.moduleHash(m.Path, m.Version)
if _, ok := c.Modules[hash]; ok {
fmt.Fprintf(os.Stderr, "WARN: duplicate module entry detected: %v\n", m)
continue
}
c.Modules[hash] = &Module{
Path: m.Path,
Version: m.Version,
Time: m.Time,
}
}
return nil
}
func (c *Cache) moduleHash(path string, version *semver.Version) string {
return path + "=" + version.String()
}
func newFilePersistence(filePath string) (*filePersistence, error) {
if filePath == "" {
var err error
if filePath, err = GetDefaultCacheBasePath(); err != nil {
return nil, err
}
filePath = filepath.Join(filePath, defaultCacheFileName)
}
// The function does an os.Stat under the hood anyway, so there's no gain in pre-checking this step.
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
return nil, err
}
// #nosec G304
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0o600)
if err != nil {
return nil, err
}
return &filePersistence{file: f}, nil
}
type filePersistence struct {
file *os.File
}
func (f filePersistence) Save(module persistedModule) error {
return json.NewEncoder(f.file).Encode(module)
}
func (f filePersistence) Load() ([]persistedModule, error) {
dec := json.NewDecoder(f.file)
modules := make([]persistedModule, 0)
for {
var m persistedModule
if err := dec.Decode(&m); err != nil {
if err == io.EOF {
break
}
return nil, err
}
modules = append(modules, m)
}
return modules, nil
}
func GetDefaultCacheBasePath() (string, error) {
filePath, envSet := os.LookupEnv("XDG_CACHE_HOME")
if envSet {
return filepath.Join(filePath, ProgramName), nil
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".config", ProgramName), nil
}