kitty/tools/utils/cached_values.go
2023-02-14 21:33:21 +05:30

41 lines
754 B
Go

// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
package utils
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
var _ = fmt.Print
type CachedValues[T any] struct {
Name string
Opts T
}
func (self *CachedValues[T]) Path() string {
return filepath.Join(CacheDir(), self.Name+".json")
}
func (self *CachedValues[T]) Load() T {
raw, err := os.ReadFile(self.Path())
if err == nil {
json.Unmarshal(raw, self.Opts)
}
return self.Opts
}
func (self *CachedValues[T]) Save() {
raw, err := json.Marshal(self.Opts)
if err == nil {
AtomicUpdateFile(self.Path(), raw, 0o600)
}
}
func NewCachedValues[T any](name string, initial_val T) *CachedValues[T] {
return &CachedValues[T]{Name: name, Opts: initial_val}
}