diff --git a/pkg/cache/config.go b/pkg/cache/config.go index bae0ecb17a..fc558a5611 100644 --- a/pkg/cache/config.go +++ b/pkg/cache/config.go @@ -4,7 +4,6 @@ import ( "time" "github.com/SigNoz/signoz/pkg/factory" - go_cache "github.com/patrickmn/go-cache" ) type Memory struct { @@ -33,8 +32,8 @@ func newConfig() factory.Config { return &Config{ Provider: "memory", Memory: Memory{ - TTL: go_cache.NoExpiration, - CleanupInterval: 1 * time.Minute, + TTL: time.Hour * 168, + CleanupInterval: 10 * time.Minute, }, Redis: Redis{ Host: "localhost", diff --git a/pkg/cache/memorycache/provider.go b/pkg/cache/memorycache/provider.go index 95fe12446d..2ff40e8266 100644 --- a/pkg/cache/memorycache/provider.go +++ b/pkg/cache/memorycache/provider.go @@ -15,7 +15,9 @@ import ( ) type provider struct { - cc *go_cache.Cache + cc *go_cache.Cache + config cache.Config + settings factory.ScopedProviderSettings } func NewFactory() factory.ProviderFactory[cache.Cache, cache.Config] { @@ -23,21 +25,25 @@ func NewFactory() factory.ProviderFactory[cache.Cache, cache.Config] { } func New(ctx context.Context, settings factory.ProviderSettings, config cache.Config) (cache.Cache, error) { - return &provider{cc: go_cache.New(config.Memory.TTL, config.Memory.CleanupInterval)}, nil + scopedProviderSettings := factory.NewScopedProviderSettings(settings, "github.com/SigNoz/signoz/pkg/cache/memorycache") + return &provider{cc: go_cache.New(config.Memory.TTL, config.Memory.CleanupInterval), settings: scopedProviderSettings, config: config}, nil } -func (c *provider) Set(_ context.Context, orgID valuer.UUID, cacheKey string, data cachetypes.Cacheable, ttl time.Duration) error { +func (provider *provider) Set(ctx context.Context, orgID valuer.UUID, cacheKey string, data cachetypes.Cacheable, ttl time.Duration) error { // check if the data being passed is a pointer and is not nil err := cachetypes.ValidatePointer(data, "inmemory") if err != nil { return err } - c.cc.Set(strings.Join([]string{orgID.StringValue(), cacheKey}, "::"), data, ttl) + if ttl == 0 { + provider.settings.Logger().WarnContext(ctx, "zero value for TTL found. defaulting to the base TTL", "cacheKey", cacheKey, "defaultTTL", provider.config.Memory.TTL) + } + provider.cc.Set(strings.Join([]string{orgID.StringValue(), cacheKey}, "::"), data, ttl) return nil } -func (c *provider) Get(_ context.Context, orgID valuer.UUID, cacheKey string, dest cachetypes.Cacheable, allowExpired bool) error { +func (provider *provider) Get(_ context.Context, orgID valuer.UUID, cacheKey string, dest cachetypes.Cacheable, allowExpired bool) error { // check if the destination being passed is a pointer and is not nil err := cachetypes.ValidatePointer(dest, "inmemory") if err != nil { @@ -50,7 +56,7 @@ func (c *provider) Get(_ context.Context, orgID valuer.UUID, cacheKey string, de return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "destination value is not settable, %s", dstv.Elem()) } - data, found := c.cc.Get(strings.Join([]string{orgID.StringValue(), cacheKey}, "::")) + data, found := provider.cc.Get(strings.Join([]string{orgID.StringValue(), cacheKey}, "::")) if !found { return errors.Newf(errors.TypeNotFound, errors.CodeNotFound, "key miss") } @@ -66,12 +72,12 @@ func (c *provider) Get(_ context.Context, orgID valuer.UUID, cacheKey string, de return nil } -func (c *provider) Delete(_ context.Context, orgID valuer.UUID, cacheKey string) { - c.cc.Delete(strings.Join([]string{orgID.StringValue(), cacheKey}, "::")) +func (provider *provider) Delete(_ context.Context, orgID valuer.UUID, cacheKey string) { + provider.cc.Delete(strings.Join([]string{orgID.StringValue(), cacheKey}, "::")) } -func (c *provider) DeleteMany(_ context.Context, orgID valuer.UUID, cacheKeys []string) { +func (provider *provider) DeleteMany(_ context.Context, orgID valuer.UUID, cacheKeys []string) { for _, cacheKey := range cacheKeys { - c.cc.Delete(strings.Join([]string{orgID.StringValue(), cacheKey}, "::")) + provider.cc.Delete(strings.Join([]string{orgID.StringValue(), cacheKey}, "::")) } }