-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.go
145 lines (119 loc) · 2.69 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
package c_polygonid
import (
"errors"
"log/slog"
"os"
"path"
"path/filepath"
"sync"
"github.com/dgraph-io/badger/v4"
)
var badgerLogger badger.Logger = nil
var openedDBs = make(map[string]*badger.DB)
var openedDBsCnts = make(map[string]int)
var dbCond = sync.NewCond(&sync.Mutex{})
func CleanCache(cacheDir string) (err error) {
cacheDir, err = createNormalizedCacheDir(cacheDir)
if err != nil {
return err
}
dbCond.L.Lock()
for openedDBsCnts[cacheDir] != 0 {
dbCond.Wait()
}
defer dbCond.L.Unlock()
db, err := openDB(cacheDir)
if err != nil {
return err
}
defer func() {
err2 := db.Close()
if err2 != nil {
if err == nil {
err = err2
} else {
slog.Error("failed to close db", "err", err2)
}
}
}()
return db.DropAll()
}
// Normalize the directory path and create it if it doesn't exist.
func createNormalizedCacheDir(d string) (string, error) {
if d == "" {
var err error
d, err = os.UserCacheDir()
if err != nil {
return "", err
}
}
normDir, err := filepath.EvalSymlinks(d)
if errors.Is(err, os.ErrNotExist) {
err = os.MkdirAll(d, 0700)
if err != nil {
return "", err
}
normDir, err = filepath.EvalSymlinks(d)
}
return normDir, err
}
func getCacheDB(cacheDir string) (*badger.DB, func(), error) {
var err error
cacheDir, err = createNormalizedCacheDir(cacheDir)
if err != nil {
return nil, nil, err
}
dbCond.L.Lock()
defer dbCond.L.Unlock()
err = maybeOpenDB(cacheDir)
if err != nil {
return nil, nil, err
}
// Close the DB only once per calling getCacheDB().
var once sync.Once
releaseDB := func() {
once.Do(func() {
dbCond.L.Lock()
defer dbCond.L.Unlock()
cnt := openedDBsCnts[cacheDir]
cnt--
if cnt == 0 {
err2 := openedDBs[cacheDir].Close()
if err2 != nil {
slog.Error("failed to close db", "err", err2)
}
delete(openedDBs, cacheDir)
delete(openedDBsCnts, cacheDir)
} else {
openedDBsCnts[cacheDir] = cnt
}
dbCond.Broadcast()
})
}
return openedDBs[cacheDir], releaseDB, nil
}
// If openedDBs[cacheDir] is nil, open a new DB and increment
// openedDBsCnts[cacheDir].
// DANGER: This function is not thread-safe and should be called only when
// dbCond.L is locked. Use getCacheDB() instead.
func maybeOpenDB(cacheDir string) error {
if openedDBs[cacheDir] != nil {
openedDBsCnts[cacheDir]++
return nil
}
db, err := openDB(cacheDir)
if err != nil {
return err
}
openedDBs[cacheDir] = db
openedDBsCnts[cacheDir]++
return nil
}
func openDB(cacheDir string) (*badger.DB, error) {
badgerPath := path.Join(cacheDir, "c-polygonid-cache")
opts := badger.DefaultOptions(badgerPath)
if badgerLogger != nil {
opts.Logger = badgerLogger
}
return badger.Open(opts)
}