-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
42 lines (35 loc) · 759 Bytes
/
Copy pathcache.go
File metadata and controls
42 lines (35 loc) · 759 Bytes
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
package main
import (
"os"
"path/filepath"
"time"
"github.com/adrg/xdg"
)
func CachePath(rel string) string {
path := filepath.Join(xdg.CacheHome, "wthis", rel)
_ = os.MkdirAll(filepath.Dir(path), 0o755)
return path
}
func ReadCache(relPath string, ttl time.Duration) (data []byte, cacheHit bool) {
path := CachePath(relPath)
info, err := os.Stat(path)
if err != nil {
return nil, false
}
if time.Since(info.ModTime()) > ttl {
return nil, false
}
b, err := os.ReadFile(path)
if err != nil {
return nil, false
}
return b, true
}
func WriteCache(relPath string, data []byte) error {
path := CachePath(relPath)
return os.WriteFile(path, data, 0o644)
}
func ClearCache() error {
path := CachePath("")
return os.RemoveAll(path)
}