sync of changes
This commit is contained in:
51
pkg/cache/cache.go
vendored
51
pkg/cache/cache.go
vendored
@@ -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))
|
||||
}
|
||||
|
Reference in New Issue
Block a user