diff options
| author | Eltrac <hi@guhub.cn> | 2025-11-21 13:46:10 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-11-21 13:46:10 +0800 |
| commit | 5205a76de15203655f9807578c8b5586f98a82bc (patch) | |
| tree | b22d0aeead6000d364a8bf1bb42516846b0a8b2b /cache.go | |
| parent | 963b66e9e7047a188cc31319212d21a78cff49b0 (diff) | |
perf: cache stats for fasterlookup
Diffstat (limited to 'cache.go')
| -rw-r--r-- | cache.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/cache.go b/cache.go new file mode 100644 index 0000000..d0b11e5 --- /dev/null +++ b/cache.go @@ -0,0 +1,37 @@ +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) ([]byte, 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) +} |
