mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-24 00:28:18 +00:00
f680dd934d
* chore: rename package errors to zerrors * rename package errors to gerrors * fix error related linting issues * fix zitadel error assertion * fix gosimple linting issues * fix deprecated linting issues * resolve gci linting issues * fix import structure --------- Co-authored-by: Elio Bischof <elio@zitadel.com>
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package fastcache
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
"reflect"
|
|
|
|
"github.com/VictoriaMetrics/fastcache"
|
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
type Fastcache struct {
|
|
cache *fastcache.Cache
|
|
}
|
|
|
|
func NewFastcache(config *Config) (*Fastcache, error) {
|
|
return &Fastcache{
|
|
cache: fastcache.New(config.MaxCacheSizeInByte),
|
|
}, nil
|
|
}
|
|
|
|
func (fc *Fastcache) Set(key string, object interface{}) error {
|
|
if key == "" || reflect.ValueOf(object).IsNil() {
|
|
return zerrors.ThrowInvalidArgument(nil, "FASTC-87dj3", "key or value should not be empty")
|
|
}
|
|
var b bytes.Buffer
|
|
enc := gob.NewEncoder(&b)
|
|
if err := enc.Encode(object); err != nil {
|
|
return zerrors.ThrowInvalidArgument(err, "FASTC-RUyxI", "unable to encode object")
|
|
}
|
|
fc.cache.Set([]byte(key), b.Bytes())
|
|
return nil
|
|
}
|
|
|
|
func (fc *Fastcache) Get(key string, ptrToObject interface{}) error {
|
|
if key == "" || reflect.ValueOf(ptrToObject).IsNil() {
|
|
return zerrors.ThrowInvalidArgument(nil, "FASTC-di8es", "key or value should not be empty")
|
|
}
|
|
data := fc.cache.Get(nil, []byte(key))
|
|
if len(data) == 0 {
|
|
return zerrors.ThrowNotFound(nil, "FASTC-xYzSm", "key not found")
|
|
}
|
|
|
|
b := bytes.NewBuffer(data)
|
|
dec := gob.NewDecoder(b)
|
|
|
|
return dec.Decode(ptrToObject)
|
|
}
|
|
|
|
func (fc *Fastcache) Delete(key string) error {
|
|
if key == "" {
|
|
return zerrors.ThrowInvalidArgument(nil, "FASTC-lod92", "key should not be empty")
|
|
}
|
|
fc.cache.Del([]byte(key))
|
|
return nil
|
|
}
|