github.com/grafana/tanka@v0.26.1-0.20240506093700-c22cfc35c21a/pkg/jsonnet/evalcache.go (about) 1 package jsonnet 2 3 import ( 4 "os" 5 "path/filepath" 6 ) 7 8 // FileEvalCache is an evaluation cache that stores its data on the local filesystem 9 type FileEvalCache struct { 10 Directory string 11 } 12 13 func NewFileEvalCache(cachePath string) *FileEvalCache { 14 return &FileEvalCache{ 15 Directory: cachePath, 16 } 17 } 18 19 func (c *FileEvalCache) cachePath(hash string) (string, error) { 20 return filepath.Abs(filepath.Join(c.Directory, hash+".json")) 21 } 22 23 func (c *FileEvalCache) Get(hash string) (string, error) { 24 cachePath, err := c.cachePath(hash) 25 if err != nil { 26 return "", err 27 } 28 29 if bytes, err := os.ReadFile(cachePath); err == nil { 30 return string(bytes), err 31 } else if !os.IsNotExist(err) { 32 return "", err 33 } 34 return "", nil 35 } 36 37 func (c *FileEvalCache) Store(hash, content string) error { 38 if err := os.MkdirAll(c.Directory, os.ModePerm); err != nil { 39 return err 40 } 41 42 cachePath, err := c.cachePath(hash) 43 if err != nil { 44 return err 45 } 46 47 return os.WriteFile(cachePath, []byte(content), 0644) 48 }