chore(cache): use sensible defaults for caching (#8011)

* chore(cache): use sensible defaults for caching

* chore(cache): initialize the cache for http handlers

* chore(cache): revert cache DI
This commit is contained in:
Vikrant Gupta 2025-05-22 17:16:09 +05:30 committed by GitHub
parent 16140991be
commit 0bfe53a93c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 13 deletions

5
pkg/cache/config.go vendored
View File

@ -4,7 +4,6 @@ import (
"time" "time"
"github.com/SigNoz/signoz/pkg/factory" "github.com/SigNoz/signoz/pkg/factory"
go_cache "github.com/patrickmn/go-cache"
) )
type Memory struct { type Memory struct {
@ -33,8 +32,8 @@ func newConfig() factory.Config {
return &Config{ return &Config{
Provider: "memory", Provider: "memory",
Memory: Memory{ Memory: Memory{
TTL: go_cache.NoExpiration, TTL: time.Hour * 168,
CleanupInterval: 1 * time.Minute, CleanupInterval: 10 * time.Minute,
}, },
Redis: Redis{ Redis: Redis{
Host: "localhost", Host: "localhost",

View File

@ -15,7 +15,9 @@ import (
) )
type provider struct { 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] { 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) { 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 // check if the data being passed is a pointer and is not nil
err := cachetypes.ValidatePointer(data, "inmemory") err := cachetypes.ValidatePointer(data, "inmemory")
if err != nil { if err != nil {
return err 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 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 // check if the destination being passed is a pointer and is not nil
err := cachetypes.ValidatePointer(dest, "inmemory") err := cachetypes.ValidatePointer(dest, "inmemory")
if err != nil { 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()) 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 { if !found {
return errors.Newf(errors.TypeNotFound, errors.CodeNotFound, "key miss") 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 return nil
} }
func (c *provider) Delete(_ context.Context, orgID valuer.UUID, cacheKey string) { func (provider *provider) Delete(_ context.Context, orgID valuer.UUID, cacheKey string) {
c.cc.Delete(strings.Join([]string{orgID.StringValue(), cacheKey}, "::")) 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 { for _, cacheKey := range cacheKeys {
c.cc.Delete(strings.Join([]string{orgID.StringValue(), cacheKey}, "::")) provider.cc.Delete(strings.Join([]string{orgID.StringValue(), cacheKey}, "::"))
} }
} }