SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
refactor(cache): move to provider pattern (#6837)
Vibhu Pandeyc92ef53e9c8c
diff --git a/pkg/cache/strategy/memory/memory.go b/pkg/cache/memorycache/provider.gosimilarity index 69%rename from pkg/cache/strategy/memory/memory.gorename to pkg/cache/memorycache/provider.goindex 5649eecf54..dc02bfc45f 100644--- a/pkg/cache/strategy/memory/memory.go+++ b/pkg/cache/memorycache/provider.go@@ -1,4 +1,4 @@-package memory+package memorycacheimport ("context"@@ -10,21 +10,21 @@ import (_cache "go.signoz.io/signoz/pkg/cache")-type cache struct {+type provider struct {cc *go_cache.Cache}-func New(opts *_cache.Memory) *cache {- return &cache{cc: go_cache.New(opts.TTL, opts.CleanupInterval)}+func New(opts *_cache.Memory) *provider {+ return &provider{cc: go_cache.New(opts.TTL, opts.CleanupInterval)}}// Connect does nothing-func (c *cache) Connect(_ context.Context) error {+func (c *provider) Connect(_ context.Context) error {return nil}// Store stores the data in the cache-func (c *cache) Store(_ context.Context, cacheKey string, data _cache.CacheableEntity, ttl time.Duration) error {+func (c *provider) Store(_ context.Context, cacheKey string, data _cache.CacheableEntity, ttl time.Duration) error {// check if the data being passed is a pointer and is not nilrv := reflect.ValueOf(data)if rv.Kind() != reflect.Pointer || rv.IsNil() {@@ -36,7 +36,7 @@ func (c *cache) Store(_ context.Context, cacheKey string, data _cache.CacheableE}// Retrieve retrieves the data from the cache-func (c *cache) Retrieve(_ context.Context, cacheKey string, dest _cache.CacheableEntity, allowExpired bool) (_cache.RetrieveStatus, error) {+func (c *provider) Retrieve(_ context.Context, cacheKey string, dest _cache.CacheableEntity, allowExpired bool) (_cache.RetrieveStatus, error) {// check if the destination being passed is a pointer and is not nildstv := reflect.ValueOf(dest)if dstv.Kind() != reflect.Pointer || dstv.IsNil() {@@ -65,7 +65,7 @@ func (c *cache) Retrieve(_ context.Context, cacheKey string, dest _cache.Cacheab}// SetTTL sets the TTL for the cache entry-func (c *cache) SetTTL(_ context.Context, cacheKey string, ttl time.Duration) {+func (c *provider) SetTTL(_ context.Context, cacheKey string, ttl time.Duration) {item, found := c.cc.Get(cacheKey)if !found {return@@ -74,23 +74,23 @@ func (c *cache) SetTTL(_ context.Context, cacheKey string, ttl time.Duration) {}// Remove removes the cache entry-func (c *cache) Remove(_ context.Context, cacheKey string) {+func (c *provider) Remove(_ context.Context, cacheKey string) {c.cc.Delete(cacheKey)}// BulkRemove removes the cache entries-func (c *cache) BulkRemove(_ context.Context, cacheKeys []string) {+func (c *provider) BulkRemove(_ context.Context, cacheKeys []string) {for _, cacheKey := range cacheKeys {c.cc.Delete(cacheKey)}}// Close does nothing-func (c *cache) Close(_ context.Context) error {+func (c *provider) Close(_ context.Context) error {return nil}// Configuration returns the cache configuration-func (c *cache) Configuration() *_cache.Memory {+func (c *provider) Configuration() *_cache.Memory {return nil}diff --git a/pkg/cache/strategy/memory/memory_test.go b/pkg/cache/memorycache/provider_test.gosimilarity index 99%rename from pkg/cache/strategy/memory/memory_test.gorename to pkg/cache/memorycache/provider_test.goindex d8434e6b2e..c3767760ed 100644--- a/pkg/cache/strategy/memory/memory_test.go+++ b/pkg/cache/memorycache/provider_test.go@@ -1,4 +1,4 @@-package memory+package memorycacheimport ("context"diff --git a/pkg/cache/strategy/redis/redis.go b/pkg/cache/rediscache/provider.gosimilarity index 64%rename from pkg/cache/strategy/redis/redis.gorename to pkg/cache/rediscache/provider.goindex 0309072656..13ced54227 100644--- a/pkg/cache/strategy/redis/redis.go+++ b/pkg/cache/rediscache/provider.go@@ -1,4 +1,4 @@-package redis+package rediscacheimport ("context"@@ -11,22 +11,22 @@ import ("go.uber.org/zap")-type cache struct {+type provider struct {client *redis.Clientopts *_cache.Redis}-func New(opts *_cache.Redis) *cache {- return &cache{opts: opts}+func New(opts *_cache.Redis) *provider {+ return &provider{opts: opts}}// WithClient creates a new cache with the given client-func WithClient(client *redis.Client) *cache {- return &cache{client: client}+func WithClient(client *redis.Client) *provider {+ return &provider{client: client}}// Connect connects to the redis server-func (c *cache) Connect(_ context.Context) error {+func (c *provider) Connect(_ context.Context) error {c.client = redis.NewClient(&redis.Options{Addr: fmt.Sprintf("%s:%d", c.opts.Host, c.opts.Port),Password: c.opts.Password,@@ -36,12 +36,12 @@ func (c *cache) Connect(_ context.Context) error {}// Store stores the data in the cache-func (c *cache) Store(ctx context.Context, cacheKey string, data _cache.CacheableEntity, ttl time.Duration) error {+func (c *provider) Store(ctx context.Context, cacheKey string, data _cache.CacheableEntity, ttl time.Duration) error {return c.client.Set(ctx, cacheKey, data, ttl).Err()}// Retrieve retrieves the data from the cache-func (c *cache) Retrieve(ctx context.Context, cacheKey string, dest _cache.CacheableEntity, allowExpired bool) (_cache.RetrieveStatus, error) {+func (c *provider) Retrieve(ctx context.Context, cacheKey string, dest _cache.CacheableEntity, allowExpired bool) (_cache.RetrieveStatus, error) {err := c.client.Get(ctx, cacheKey).Scan(dest)if err != nil {if errors.Is(err, redis.Nil) {@@ -53,7 +53,7 @@ func (c *cache) Retrieve(ctx context.Context, cacheKey string, dest _cache.Cache}// SetTTL sets the TTL for the cache entry-func (c *cache) SetTTL(ctx context.Context, cacheKey string, ttl time.Duration) {+func (c *provider) SetTTL(ctx context.Context, cacheKey string, ttl time.Duration) {err := c.client.Expire(ctx, cacheKey, ttl).Err()if err != nil {zap.L().Error("error setting TTL for cache key", zap.String("cacheKey", cacheKey), zap.Duration("ttl", ttl), zap.Error(err))@@ -61,39 +61,39 @@ func (c *cache) SetTTL(ctx context.Context, cacheKey string, ttl time.Duration)}// Remove removes the cache entry-func (c *cache) Remove(ctx context.Context, cacheKey string) {+func (c *provider) Remove(ctx context.Context, cacheKey string) {c.BulkRemove(ctx, []string{cacheKey})}// BulkRemove removes the cache entries-func (c *cache) BulkRemove(ctx context.Context, cacheKeys []string) {+func (c *provider) BulkRemove(ctx context.Context, cacheKeys []string) {if err := c.client.Del(ctx, cacheKeys...).Err(); err != nil {zap.L().Error("error deleting cache keys", zap.Strings("cacheKeys", cacheKeys), zap.Error(err))}}// Close closes the connection to the redis server-func (c *cache) Close(_ context.Context) error {+func (c *provider) Close(_ context.Context) error {return c.client.Close()}// Ping pings the redis server-func (c *cache) Ping(ctx context.Context) error {+func (c *provider) Ping(ctx context.Context) error {return c.client.Ping(ctx).Err()}// GetClient returns the redis client-func (c *cache) GetClient() *redis.Client {+func (c *provider) GetClient() *redis.Client {return c.client}// GetOptions returns the options-func (c *cache) GetOptions() *_cache.Redis {+func (c *provider) GetOptions() *_cache.Redis {return c.opts}// GetTTL returns the TTL for the cache entry-func (c *cache) GetTTL(ctx context.Context, cacheKey string) time.Duration {+func (c *provider) GetTTL(ctx context.Context, cacheKey string) time.Duration {ttl, err := c.client.TTL(ctx, cacheKey).Result()if err != nil {zap.L().Error("error getting TTL for cache key", zap.String("cacheKey", cacheKey), zap.Error(err))@@ -102,12 +102,12 @@ func (c *cache) GetTTL(ctx context.Context, cacheKey string) time.Duration {}// GetKeys returns the keys matching the pattern-func (c *cache) GetKeys(ctx context.Context, pattern string) ([]string, error) {+func (c *provider) GetKeys(ctx context.Context, pattern string) ([]string, error) {return c.client.Keys(ctx, pattern).Result()}// GetKeysWithTTL returns the keys matching the pattern with their TTL-func (c *cache) GetKeysWithTTL(ctx context.Context, pattern string) (map[string]time.Duration, error) {+func (c *provider) GetKeysWithTTL(ctx context.Context, pattern string) (map[string]time.Duration, error) {keys, err := c.GetKeys(ctx, pattern)if err != nil {return nil, errdiff --git a/pkg/cache/strategy/redis/redis_test.go b/pkg/cache/rediscache/provider_test.gosimilarity index 99%rename from pkg/cache/strategy/redis/redis_test.gorename to pkg/cache/rediscache/provider_test.goindex 2b1539f2bd..acc447ae74 100644--- a/pkg/cache/strategy/redis/redis_test.go+++ b/pkg/cache/rediscache/provider_test.go@@ -1,4 +1,4 @@-package redis+package rediscacheimport ("context"diff --git a/pkg/signoz/signoz.go b/pkg/signoz/signoz.goindex a2a42d3073..eaf13be2d6 100644--- a/pkg/signoz/signoz.go+++ b/pkg/signoz/signoz.go@@ -2,8 +2,8 @@ package signozimport ("go.signoz.io/signoz/pkg/cache"- "go.signoz.io/signoz/pkg/cache/strategy/memory"- "go.signoz.io/signoz/pkg/cache/strategy/redis"+ "go.signoz.io/signoz/pkg/cache/memorycache"+ "go.signoz.io/signoz/pkg/cache/rediscache""go.signoz.io/signoz/pkg/config""go.signoz.io/signoz/pkg/web""go.uber.org/zap"@@ -20,9 +20,9 @@ func New(config *config.Config, skipWebFrontend bool) (*SigNoz, error) {// init for the cacheswitch config.Cache.Provider {case "memory":- cache = memory.New(&config.Cache.Memory)+ cache = memorycache.New(&config.Cache.Memory)case "redis":- cache = redis.New(&config.Cache.Redis)+ cache = rediscache.New(&config.Cache.Redis)}web, err := web.New(zap.L(), config.Web)