sync of changes

This commit is contained in:
2024-11-09 19:50:14 -05:00
parent cc7b03c614
commit a83d5f9822
9 changed files with 491 additions and 95 deletions

51
pkg/cache/cache.go vendored
View File

@@ -1,6 +1,16 @@
package cache
import "io"
import (
"crypto/sha256"
"errors"
"fmt"
"io"
)
var (
// ErrNotFound is returned when the key is not found in the cache
ErrNotFound = errors.New("key not found")
)
type Cache interface {
Get(key string, writer io.Writer) error
@@ -13,3 +23,42 @@ type Cache interface {
Delete(key string) error
}
type ShaWrapper struct {
Cache Cache
}
func (s ShaWrapper) hash(key string) string {
// hash the key to a sha256
hash := sha256.Sum256([]byte(key))
// return the hex representation of the hash
return fmt.Sprintf("%x", hash)
}
func (s ShaWrapper) Get(key string, writer io.Writer) error {
return s.Cache.Get(s.hash(key), writer)
}
func (s ShaWrapper) GetString(key string) (string, error) {
return s.Cache.GetString(s.hash(key))
}
func (s ShaWrapper) GetJSON(key string, value any) error {
return s.Cache.GetJSON(s.hash(key), value)
}
func (s ShaWrapper) Set(key string, value io.Reader) error {
return s.Cache.Set(s.hash(key), value)
}
func (s ShaWrapper) SetJSON(key string, value any) error {
return s.Cache.SetJSON(s.hash(key), value)
}
func (s ShaWrapper) SetString(key string, value string) error {
return s.Cache.SetString(s.hash(key), value)
}
func (s ShaWrapper) Delete(key string) error {
return s.Cache.Delete(s.hash(key))
}

View File

@@ -14,8 +14,7 @@ import (
type Directory struct {
BaseFolder string
MaxLife time.Duration
lock sync.Mutex
lock sync.Mutex
}
var _ Cache = &Directory{}
@@ -76,7 +75,16 @@ func (d *Directory) AutoCleanupRoutine(ctx context.Context) error {
func (d *Directory) openFile(key string) (*os.File, error) {
path := d.GetPath(key)
return os.Open(path)
res, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return nil, ErrNotFound
}
return nil, err
}
return res, nil
}
func (d *Directory) Set(key string, value io.Reader) error {