You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have preliminary work on this but it is not tested, vetted, and possibly is missing functionality from otter that would be useful. It is based on the ristretto store.
package cache
import (
"context""errors""fmt""strings""time"
lib_store "github.com/eko/gocache/lib/v4/store""github.com/maypok86/otter"
)
const (
// OtterType represents the storage type as a string valueOtterType="otter"// OtterTagPattern represents the tag pattern to be used as a key in specified storageOtterTagPattern="gocache_tag_%s"
)
// OtterClientInterface represents a maypok86/otter clienttypeOtterClientInterfaceinterface {
Get(keystring) (any, bool)
Set(keystring, valueany, ttl time.Duration) boolDelete(keystring)
Clear()
}
var_OtterClientInterface=new(otter.CacheWithVariableTTL[string, any])
// OtterStore is a store for Otter (memory) librarytypeOtterStorestruct {
clientOtterClientInterfaceoptions*lib_store.Options
}
// NewOtter creates a new store to Otter (memory) library instancefuncNewOtter(clientOtterClientInterface, options...lib_store.Option) *OtterStore {
return&OtterStore{
client: client,
options: lib_store.ApplyOptions(options...),
}
}
// Get returns data stored from a given keyfunc (s*OtterStore) Get(_ context.Context, keyany) (any, error) {
varerrerrorvalue, exists:=s.client.Get(key.(string))
if!exists {
err=lib_store.NotFoundWithCause(errors.New("value not found in Otter store"))
}
returnvalue, err
}
// GetWithTTL returns data stored from a given key and its corresponding TTLfunc (s*OtterStore) GetWithTTL(ctx context.Context, keyany) (any, time.Duration, error) {
value, err:=s.Get(ctx, key)
returnvalue, 0, err
}
// Set defines data in Otter memory cache for given key identifierfunc (s*OtterStore) Set(ctx context.Context, keyany, valueany, options...lib_store.Option) error {
opts:=lib_store.ApplyOptionsWithDefault(s.options, options...)
varerrerrorifset:=s.client.Set(key.(string), value, opts.Expiration); !set {
err=fmt.Errorf("error occurred while setting value '%v' on key '%v'", value, key)
}
iferr!=nil {
returnerr
}
iftags:=opts.Tags; len(tags) >0 {
s.setTags(ctx, key, tags)
}
returnnil
}
func (s*OtterStore) setTags(ctx context.Context, keyany, tags []string) {
for_, tag:=rangetags {
tagKey:=fmt.Sprintf(OtterTagPattern, tag)
varcacheKeys []stringifresult, err:=s.Get(ctx, tagKey); err==nil {
ifbytes, ok:=result.([]byte); ok {
cacheKeys=strings.Split(string(bytes), ",")
}
}
alreadyInserted:=falsefor_, cacheKey:=rangecacheKeys {
ifcacheKey==key.(string) {
alreadyInserted=truebreak
}
}
if!alreadyInserted {
cacheKeys=append(cacheKeys, key.(string))
}
_=s.Set(ctx, tagKey, []byte(strings.Join(cacheKeys, ",")), lib_store.WithExpiration(720*time.Hour))
}
}
// Delete removes data in Otter memory cache for given key identifierfunc (s*OtterStore) Delete(_ context.Context, keyany) error {
s.client.Delete(key.(string))
returnnil
}
// Invalidate invalidates some cache data in Otter for given optionsfunc (s*OtterStore) Invalidate(ctx context.Context, options...lib_store.InvalidateOption) error {
opts:=lib_store.ApplyInvalidateOptions(options...)
iftags:=opts.Tags; len(tags) >0 {
for_, tag:=rangetags {
tagKey:=fmt.Sprintf(OtterTagPattern, tag)
result, err:=s.Get(ctx, tagKey)
iferr!=nil {
returnnil
}
varcacheKeys []stringifbytes, ok:=result.([]byte); ok {
cacheKeys=strings.Split(string(bytes), ",")
}
for_, cacheKey:=rangecacheKeys {
_=s.Delete(ctx, cacheKey)
}
}
}
returnnil
}
// Clear resets all data in the storefunc (s*OtterStore) Clear(_ context.Context) error {
s.client.Clear()
returnnil
}
// GetType returns the store typefunc (s*OtterStore) GetType() string {
returnOtterType
}
I have preliminary work on this but it is not tested, vetted, and possibly is missing functionality from otter that would be useful. It is based on the ristretto store.
I am using this as such:
The text was updated successfully, but these errors were encountered: