2025-01-17 18:09:39 +05:30
|
|
|
package memorycache
|
2025-01-04 01:28:54 +05:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"reflect"
|
2025-05-03 18:30:07 +05:30
|
|
|
"strings"
|
2025-01-04 01:28:54 +05:30
|
|
|
"time"
|
|
|
|
|
|
2025-03-20 21:01:41 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/cache"
|
2025-05-03 18:30:07 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/errors"
|
2025-03-20 21:01:41 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/factory"
|
2025-05-03 18:30:07 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/types/cachetypes"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/valuer"
|
2025-07-05 19:08:23 +05:30
|
|
|
gocache "github.com/patrickmn/go-cache"
|
2025-01-04 01:28:54 +05:30
|
|
|
)
|
|
|
|
|
|
2025-01-17 18:09:39 +05:30
|
|
|
type provider struct {
|
2025-07-05 19:08:23 +05:30
|
|
|
cc *gocache.Cache
|
2025-05-22 17:16:09 +05:30
|
|
|
config cache.Config
|
|
|
|
|
settings factory.ScopedProviderSettings
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-01-20 17:45:33 +05:30
|
|
|
func NewFactory() factory.ProviderFactory[cache.Cache, cache.Config] {
|
|
|
|
|
return factory.NewProviderFactory(factory.MustNewName("memory"), New)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New(ctx context.Context, settings factory.ProviderSettings, config cache.Config) (cache.Cache, error) {
|
2025-05-22 17:16:09 +05:30
|
|
|
scopedProviderSettings := factory.NewScopedProviderSettings(settings, "github.com/SigNoz/signoz/pkg/cache/memorycache")
|
2025-07-05 19:08:23 +05:30
|
|
|
|
|
|
|
|
return &provider{
|
|
|
|
|
cc: gocache.New(config.Memory.TTL, config.Memory.CleanupInterval),
|
|
|
|
|
settings: scopedProviderSettings,
|
|
|
|
|
config: config,
|
|
|
|
|
}, nil
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-05-22 17:16:09 +05:30
|
|
|
func (provider *provider) Set(ctx context.Context, orgID valuer.UUID, cacheKey string, data cachetypes.Cacheable, ttl time.Duration) error {
|
2025-07-05 19:08:23 +05:30
|
|
|
err := cachetypes.CheckCacheablePointer(data)
|
2025-05-03 18:30:07 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-07-05 19:08:23 +05:30
|
|
|
if cloneable, ok := data.(cachetypes.Cloneable); ok {
|
|
|
|
|
toCache := cloneable.Clone()
|
|
|
|
|
provider.cc.Set(strings.Join([]string{orgID.StringValue(), cacheKey}, "::"), toCache, ttl)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toCache, err := data.MarshalBinary()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2025-05-22 17:16:09 +05:30
|
|
|
}
|
2025-07-05 19:08:23 +05:30
|
|
|
|
|
|
|
|
provider.cc.Set(strings.Join([]string{orgID.StringValue(), cacheKey}, "::"), toCache, ttl)
|
2025-01-04 01:28:54 +05:30
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-22 17:16:09 +05:30
|
|
|
func (provider *provider) Get(_ context.Context, orgID valuer.UUID, cacheKey string, dest cachetypes.Cacheable, allowExpired bool) error {
|
2025-07-05 19:08:23 +05:30
|
|
|
err := cachetypes.CheckCacheablePointer(dest)
|
2025-05-03 18:30:07 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-07-05 19:08:23 +05:30
|
|
|
cachedData, found := provider.cc.Get(strings.Join([]string{orgID.StringValue(), cacheKey}, "::"))
|
2025-01-04 01:28:54 +05:30
|
|
|
if !found {
|
2025-05-03 18:30:07 +05:30
|
|
|
return errors.Newf(errors.TypeNotFound, errors.CodeNotFound, "key miss")
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-07-05 19:08:23 +05:30
|
|
|
if cloneable, ok := cachedData.(cachetypes.Cloneable); ok {
|
|
|
|
|
// check if the destination value is settable
|
|
|
|
|
dstv := reflect.ValueOf(dest)
|
|
|
|
|
if !dstv.Elem().CanSet() {
|
|
|
|
|
return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "unsettable: (value: \"%s\")", dstv.Elem())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fromCache := cloneable.Clone()
|
|
|
|
|
|
|
|
|
|
// check the type compatbility between the src and dest
|
|
|
|
|
srcv := reflect.ValueOf(fromCache)
|
|
|
|
|
if !srcv.Type().AssignableTo(dstv.Type()) {
|
|
|
|
|
return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "unassignable: (src: \"%s\", dst: \"%s\")", srcv.Type().String(), dstv.Type().String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set the value to from src to dest
|
|
|
|
|
dstv.Elem().Set(srcv.Elem())
|
|
|
|
|
return nil
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-07-05 19:08:23 +05:30
|
|
|
if fromCache, ok := cachedData.([]byte); ok {
|
|
|
|
|
if err = dest.UnmarshalBinary(fromCache); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return errors.NewInternalf(errors.CodeInternal, "unrecognized: (value: \"%s\")", reflect.TypeOf(cachedData).String())
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-05-22 17:16:09 +05:30
|
|
|
func (provider *provider) Delete(_ context.Context, orgID valuer.UUID, cacheKey string) {
|
|
|
|
|
provider.cc.Delete(strings.Join([]string{orgID.StringValue(), cacheKey}, "::"))
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-05-22 17:16:09 +05:30
|
|
|
func (provider *provider) DeleteMany(_ context.Context, orgID valuer.UUID, cacheKeys []string) {
|
2025-01-04 01:28:54 +05:30
|
|
|
for _, cacheKey := range cacheKeys {
|
2025-05-22 17:16:09 +05:30
|
|
|
provider.cc.Delete(strings.Join([]string{orgID.StringValue(), cacheKey}, "::"))
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
}
|