-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcache.go
More file actions
184 lines (146 loc) · 3.68 KB
/
Copy pathcache.go
File metadata and controls
184 lines (146 loc) · 3.68 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
package lunar
import (
"encoding/json"
"log"
"os"
"path/filepath"
"sync"
"syscall"
)
// Cache is the cache interface
type Cache interface {
GetItems(namespace string) Items
SetItems(namespace string, items Items) error
GetKeys() []string
Delete(namespace string) error
Drain()
}
// MemoryCache is cache stored in memory, it's the default cache for use
type MemoryCache struct {
items sync.Map // key: namespace, value: items
}
// make sure MemoryCache implements Cache
var _ Cache = new(MemoryCache)
// GetItems gets items from cache
func (c *MemoryCache) GetItems(namespace string) Items {
if v, ok := c.items.Load(namespace); ok {
return v.(Items)
}
return Items{}
}
// SetItems sets items into cache
func (c *MemoryCache) SetItems(namespace string, items Items) error {
c.items.Store(namespace, items)
return nil
}
// GetKeys gets all the keys (namespaces)
func (c *MemoryCache) GetKeys() []string {
var keys []string
c.items.Range(func(key, value interface{}) bool {
keys = append(keys, key.(string))
return true
})
return keys
}
// Delete deletes given namespace
func (c *MemoryCache) Delete(namespace string) error {
c.items.Delete(namespace)
return nil
}
// Drain deletes the whole cache
func (c *MemoryCache) Drain() {
c.items.Range(func(key, value interface{}) bool {
c.items.Delete(key)
return true
})
}
// FileCache is cache stored in files.
type FileCache struct {
lock sync.Mutex
AppID string
Folder string // root folder
Perm os.FileMode
}
// make sure FileCache implements Cache
var _ Cache = new(FileCache)
// NewFileCache creates a FileCache
func NewFileCache(appID string, folder string) *FileCache {
if folder == "" {
folder = "/tmp"
}
c := &FileCache{
AppID: appID,
Folder: folder,
Perm: 0666,
}
if err := c.createAppFolder(); err != nil {
return nil
}
return c
}
// check and create app folder if not existing
func (c *FileCache) createAppFolder() error {
if _, err := os.Stat(c.getAppFolder()); err != nil {
return os.MkdirAll(c.getAppFolder(), os.FileMode(0755))
}
return nil
}
func (c *FileCache) getAppFolder() string {
return filepath.Join(c.Folder, c.AppID)
}
// file path is {Folder}/{app id}/{namespace}
func (c *FileCache) getFilePath(namespace string) string {
return filepath.Join(c.getAppFolder(), namespace)
}
// GetItems gets items from cache
func (c *FileCache) GetItems(namespace string) Items {
c.lock.Lock()
defer c.lock.Unlock()
items := make(Items)
if data, err := os.ReadFile(c.getFilePath(namespace)); err == nil {
if IsProperties(namespace) {
if err := json.Unmarshal(data, &items); err != nil {
log.Printf("parse data error: %s", err.Error())
}
} else {
items["content"] = string(data)
}
}
return items
}
// SetItems sets items into cache
func (c *FileCache) SetItems(namespace string, items Items) error {
c.lock.Lock()
defer c.lock.Unlock()
var content string
if IsProperties(namespace) {
content = items.String()
} else {
content = items.Get("content")
}
return os.WriteFile(c.getFilePath(namespace), []byte(content), c.Perm)
}
// GetKeys gets all the keys (namespaces)
func (c *FileCache) GetKeys() []string {
c.lock.Lock()
defer c.lock.Unlock()
f, err := os.Open(filepath.Join(c.Folder, c.AppID))
if err != nil {
return nil
}
names, _ := f.Readdirnames(-1)
f.Close()
return names
}
// Delete deletes given namespace
func (c *FileCache) Delete(namespace string) error {
return syscall.Unlink(c.getFilePath(namespace))
}
// Drain deletes the whole cache
func (c *FileCache) Drain() {
for _, namespace := range c.GetKeys() {
if err := syscall.Unlink(c.getFilePath(namespace)); err != nil {
log.Printf("failed to delete cache file: %s", err.Error())
}
}
}