package cache 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 GetString(key string) (string, error) GetJSON(key string, value any) error Set(key string, value io.Reader) error SetJSON(key string, value any) error SetString(key string, value string) error 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)) }