refactor(cache): move to provider pattern (#6837)

### Summary

Move cache to provider pattern
This commit is contained in:
Vibhu Pandey 2025-01-17 18:09:39 +05:30 committed by GitHub
parent 268f283785
commit c92ef53e9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 37 additions and 37 deletions

View File

@ -1,4 +1,4 @@
package memory package memorycache
import ( import (
"context" "context"
@ -10,21 +10,21 @@ import (
_cache "go.signoz.io/signoz/pkg/cache" _cache "go.signoz.io/signoz/pkg/cache"
) )
type cache struct { type provider struct {
cc *go_cache.Cache cc *go_cache.Cache
} }
func New(opts *_cache.Memory) *cache { func New(opts *_cache.Memory) *provider {
return &cache{cc: go_cache.New(opts.TTL, opts.CleanupInterval)} return &provider{cc: go_cache.New(opts.TTL, opts.CleanupInterval)}
} }
// Connect does nothing // Connect does nothing
func (c *cache) Connect(_ context.Context) error { func (c *provider) Connect(_ context.Context) error {
return nil return nil
} }
// Store stores the data in the cache // 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 nil // check if the data being passed is a pointer and is not nil
rv := reflect.ValueOf(data) rv := reflect.ValueOf(data)
if rv.Kind() != reflect.Pointer || rv.IsNil() { 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 // 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 nil // check if the destination being passed is a pointer and is not nil
dstv := reflect.ValueOf(dest) dstv := reflect.ValueOf(dest)
if dstv.Kind() != reflect.Pointer || dstv.IsNil() { 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 // 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) item, found := c.cc.Get(cacheKey)
if !found { if !found {
return return
@ -74,23 +74,23 @@ func (c *cache) SetTTL(_ context.Context, cacheKey string, ttl time.Duration) {
} }
// Remove removes the cache entry // 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) c.cc.Delete(cacheKey)
} }
// BulkRemove removes the cache entries // 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 { for _, cacheKey := range cacheKeys {
c.cc.Delete(cacheKey) c.cc.Delete(cacheKey)
} }
} }
// Close does nothing // Close does nothing
func (c *cache) Close(_ context.Context) error { func (c *provider) Close(_ context.Context) error {
return nil return nil
} }
// Configuration returns the cache configuration // Configuration returns the cache configuration
func (c *cache) Configuration() *_cache.Memory { func (c *provider) Configuration() *_cache.Memory {
return nil return nil
} }

View File

@ -1,4 +1,4 @@
package memory package memorycache
import ( import (
"context" "context"

View File

@ -1,4 +1,4 @@
package redis package rediscache
import ( import (
"context" "context"
@ -11,22 +11,22 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
type cache struct { type provider struct {
client *redis.Client client *redis.Client
opts *_cache.Redis opts *_cache.Redis
} }
func New(opts *_cache.Redis) *cache { func New(opts *_cache.Redis) *provider {
return &cache{opts: opts} return &provider{opts: opts}
} }
// WithClient creates a new cache with the given client // WithClient creates a new cache with the given client
func WithClient(client *redis.Client) *cache { func WithClient(client *redis.Client) *provider {
return &cache{client: client} return &provider{client: client}
} }
// Connect connects to the redis server // 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{ c.client = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", c.opts.Host, c.opts.Port), Addr: fmt.Sprintf("%s:%d", c.opts.Host, c.opts.Port),
Password: c.opts.Password, Password: c.opts.Password,
@ -36,12 +36,12 @@ func (c *cache) Connect(_ context.Context) error {
} }
// Store stores the data in the cache // 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() return c.client.Set(ctx, cacheKey, data, ttl).Err()
} }
// Retrieve retrieves the data from the cache // 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) err := c.client.Get(ctx, cacheKey).Scan(dest)
if err != nil { if err != nil {
if errors.Is(err, redis.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 // 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() err := c.client.Expire(ctx, cacheKey, ttl).Err()
if err != nil { if err != nil {
zap.L().Error("error setting TTL for cache key", zap.String("cacheKey", cacheKey), zap.Duration("ttl", ttl), zap.Error(err)) 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 // 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}) c.BulkRemove(ctx, []string{cacheKey})
} }
// BulkRemove removes the cache entries // 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 { if err := c.client.Del(ctx, cacheKeys...).Err(); err != nil {
zap.L().Error("error deleting cache keys", zap.Strings("cacheKeys", cacheKeys), zap.Error(err)) zap.L().Error("error deleting cache keys", zap.Strings("cacheKeys", cacheKeys), zap.Error(err))
} }
} }
// Close closes the connection to the redis server // 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() return c.client.Close()
} }
// Ping pings the redis server // 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() return c.client.Ping(ctx).Err()
} }
// GetClient returns the redis client // GetClient returns the redis client
func (c *cache) GetClient() *redis.Client { func (c *provider) GetClient() *redis.Client {
return c.client return c.client
} }
// GetOptions returns the options // GetOptions returns the options
func (c *cache) GetOptions() *_cache.Redis { func (c *provider) GetOptions() *_cache.Redis {
return c.opts return c.opts
} }
// GetTTL returns the TTL for the cache entry // 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() ttl, err := c.client.TTL(ctx, cacheKey).Result()
if err != nil { if err != nil {
zap.L().Error("error getting TTL for cache key", zap.String("cacheKey", cacheKey), zap.Error(err)) 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 // 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() return c.client.Keys(ctx, pattern).Result()
} }
// GetKeysWithTTL returns the keys matching the pattern with their TTL // 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) keys, err := c.GetKeys(ctx, pattern)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -1,4 +1,4 @@
package redis package rediscache
import ( import (
"context" "context"

View File

@ -2,8 +2,8 @@ package signoz
import ( import (
"go.signoz.io/signoz/pkg/cache" "go.signoz.io/signoz/pkg/cache"
"go.signoz.io/signoz/pkg/cache/strategy/memory" "go.signoz.io/signoz/pkg/cache/memorycache"
"go.signoz.io/signoz/pkg/cache/strategy/redis" "go.signoz.io/signoz/pkg/cache/rediscache"
"go.signoz.io/signoz/pkg/config" "go.signoz.io/signoz/pkg/config"
"go.signoz.io/signoz/pkg/web" "go.signoz.io/signoz/pkg/web"
"go.uber.org/zap" "go.uber.org/zap"
@ -20,9 +20,9 @@ func New(config *config.Config, skipWebFrontend bool) (*SigNoz, error) {
// init for the cache // init for the cache
switch config.Cache.Provider { switch config.Cache.Provider {
case "memory": case "memory":
cache = memory.New(&config.Cache.Memory) cache = memorycache.New(&config.Cache.Memory)
case "redis": case "redis":
cache = redis.New(&config.Cache.Redis) cache = rediscache.New(&config.Cache.Redis)
} }
web, err := web.New(zap.L(), config.Web) web, err := web.New(zap.L(), config.Web)