feat: Add initial caching
References: https://todo.sr.ht/~timharek/yr/1 Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
parent
845038120e
commit
019ec38fc1
2 changed files with 96 additions and 0 deletions
69
pkg/cache/cache.go
vendored
Normal file
69
pkg/cache/cache.go
vendored
Normal file
|
@ -0,0 +1,69 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Cache[T any] struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func New[T any](path string) *Cache[T] {
|
||||
if path == "" {
|
||||
path = os.TempDir()
|
||||
}
|
||||
return &Cache[T]{path}
|
||||
}
|
||||
|
||||
// Add data as JSON-file into cache-dir, returns nil if success
|
||||
func (c *Cache[T]) Add(name string, data any) error {
|
||||
j, err := json.MarshalIndent(data, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to convert data as JSON.")
|
||||
}
|
||||
err = os.MkdirAll(c.Path, 0646)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to ensure cache-dir is present.")
|
||||
}
|
||||
|
||||
filePath := filepath.Join(c.Path, fmt.Sprintf("%s.json", name))
|
||||
d := []byte(j)
|
||||
err = os.WriteFile(filePath, d, 0646)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed write data to cache-file.")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get data from JSON-file from cache-dir, returns nil if no result
|
||||
func (c *Cache[T]) Get(name string) (*T, error) {
|
||||
filePath := filepath.Join(c.Path, fmt.Sprintf("%s.json", name))
|
||||
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
rawFile, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read cache-file.")
|
||||
}
|
||||
|
||||
var result T
|
||||
|
||||
bytes, err := io.ReadAll(rawFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read data from cache-file.")
|
||||
}
|
||||
|
||||
err = json.Unmarshal(bytes, &result)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to parse JSON from cache-file.")
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
27
pkg/cache/cache_test.go
vendored
Normal file
27
pkg/cache/cache_test.go
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type testType struct {
|
||||
Prop1 string `json:"prop1"`
|
||||
Prop2 string `json:"prop2"`
|
||||
}
|
||||
|
||||
func TestCache(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
c := New[testType]("")
|
||||
assert.Equal(os.TempDir(), c.Path)
|
||||
|
||||
err := c.Add("something", &testType{Prop1: "test1", Prop2: "test2"})
|
||||
assert.NoError(err)
|
||||
|
||||
result, err := c.Get("something")
|
||||
assert.NoError(err)
|
||||
assert.Equal("test1", result.Prop1)
|
||||
assert.Equal("test2", result.Prop2)
|
||||
}
|
Loading…
Reference in a new issue