-
Notifications
You must be signed in to change notification settings - Fork 5
/
cache_manager.go
115 lines (94 loc) · 2.22 KB
/
cache_manager.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
package freezer
import (
"encoding/gob"
"errors"
"os"
"path/filepath"
)
type CacheManager struct {
Cache CacheDB
cacheDir string
dbFile *os.File
}
type CacheDB map[string]CacheEntry
type CacheEntry struct {
Version string
URI string
}
func NewCacheManager(cacheDir string) CacheManager {
return CacheManager{
cacheDir: cacheDir,
}
}
func (c *CacheManager) Open() error {
var err error
_, err = os.Stat(filepath.Join(c.cacheDir, "buildpacks-cache.db"))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(c.cacheDir, os.ModePerm)
if err != nil {
return err
}
c.dbFile, err = os.Create(filepath.Join(c.cacheDir, "buildpacks-cache.db"))
if err != nil {
return err
}
c.Cache = CacheDB{}
return nil
}
return err
}
loadFile, err := os.Open(filepath.Join(c.cacheDir, "buildpacks-cache.db"))
if err != nil {
return err
}
err = gob.NewDecoder(loadFile).Decode(&c.Cache)
if err != nil {
return err
}
c.dbFile, err = os.OpenFile(filepath.Join(c.cacheDir, "buildpacks-cache.db"), os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
return err
}
return nil
}
func (c CacheManager) Close() error {
err := gob.NewEncoder(c.dbFile).Encode(&c.Cache)
if err != nil {
return err
}
defer c.dbFile.Close()
return nil
}
//This function exists for two reasons one is so that is could have a standard
//getter setter interface and the setter is a more complex function the other is
//to allow for table locking if this were to be adapted for parallel package management
func (c CacheManager) Get(key string) (CacheEntry, bool, error) {
entry, ok := c.Cache[key]
if ok {
_, err := os.Stat(entry.URI)
if err != nil {
if os.IsNotExist(err) {
return entry, !ok, nil
}
return CacheEntry{}, !ok, err
}
}
return entry, ok, nil
}
func (c *CacheManager) Set(key string, value CacheEntry) error {
//os.RemoveAll of a empty string is a noop if the entry does not exist then it will
//return and empty string
err := os.RemoveAll(c.Cache[key].URI)
if err != nil {
return err
}
if c.Cache == nil {
return errors.New("the cache manager is not loaded properly")
}
c.Cache[key] = value
return nil
}
func (c CacheManager) Dir() string {
return c.cacheDir
}