Skip to content

Commit c92ef53

Browse files
refactor(cache): move to provider pattern (#6837)
### Summary Move cache to provider pattern
1 parent 268f283 commit c92ef53

File tree

5 files changed

+37
-37
lines changed

5 files changed

+37
-37
lines changed

pkg/cache/strategy/memory/memory.go pkg/cache/memorycache/provider.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package memory
1+
package memorycache
22

33
import (
44
"context"
@@ -10,21 +10,21 @@ import (
1010
_cache "go.signoz.io/signoz/pkg/cache"
1111
)
1212

13-
type cache struct {
13+
type provider struct {
1414
cc *go_cache.Cache
1515
}
1616

17-
func New(opts *_cache.Memory) *cache {
18-
return &cache{cc: go_cache.New(opts.TTL, opts.CleanupInterval)}
17+
func New(opts *_cache.Memory) *provider {
18+
return &provider{cc: go_cache.New(opts.TTL, opts.CleanupInterval)}
1919
}
2020

2121
// Connect does nothing
22-
func (c *cache) Connect(_ context.Context) error {
22+
func (c *provider) Connect(_ context.Context) error {
2323
return nil
2424
}
2525

2626
// Store stores the data in the cache
27-
func (c *cache) Store(_ context.Context, cacheKey string, data _cache.CacheableEntity, ttl time.Duration) error {
27+
func (c *provider) Store(_ context.Context, cacheKey string, data _cache.CacheableEntity, ttl time.Duration) error {
2828
// check if the data being passed is a pointer and is not nil
2929
rv := reflect.ValueOf(data)
3030
if rv.Kind() != reflect.Pointer || rv.IsNil() {
@@ -36,7 +36,7 @@ func (c *cache) Store(_ context.Context, cacheKey string, data _cache.CacheableE
3636
}
3737

3838
// Retrieve retrieves the data from the cache
39-
func (c *cache) Retrieve(_ context.Context, cacheKey string, dest _cache.CacheableEntity, allowExpired bool) (_cache.RetrieveStatus, error) {
39+
func (c *provider) Retrieve(_ context.Context, cacheKey string, dest _cache.CacheableEntity, allowExpired bool) (_cache.RetrieveStatus, error) {
4040
// check if the destination being passed is a pointer and is not nil
4141
dstv := reflect.ValueOf(dest)
4242
if dstv.Kind() != reflect.Pointer || dstv.IsNil() {
@@ -65,7 +65,7 @@ func (c *cache) Retrieve(_ context.Context, cacheKey string, dest _cache.Cacheab
6565
}
6666

6767
// SetTTL sets the TTL for the cache entry
68-
func (c *cache) SetTTL(_ context.Context, cacheKey string, ttl time.Duration) {
68+
func (c *provider) SetTTL(_ context.Context, cacheKey string, ttl time.Duration) {
6969
item, found := c.cc.Get(cacheKey)
7070
if !found {
7171
return
@@ -74,23 +74,23 @@ func (c *cache) SetTTL(_ context.Context, cacheKey string, ttl time.Duration) {
7474
}
7575

7676
// Remove removes the cache entry
77-
func (c *cache) Remove(_ context.Context, cacheKey string) {
77+
func (c *provider) Remove(_ context.Context, cacheKey string) {
7878
c.cc.Delete(cacheKey)
7979
}
8080

8181
// BulkRemove removes the cache entries
82-
func (c *cache) BulkRemove(_ context.Context, cacheKeys []string) {
82+
func (c *provider) BulkRemove(_ context.Context, cacheKeys []string) {
8383
for _, cacheKey := range cacheKeys {
8484
c.cc.Delete(cacheKey)
8585
}
8686
}
8787

8888
// Close does nothing
89-
func (c *cache) Close(_ context.Context) error {
89+
func (c *provider) Close(_ context.Context) error {
9090
return nil
9191
}
9292

9393
// Configuration returns the cache configuration
94-
func (c *cache) Configuration() *_cache.Memory {
94+
func (c *provider) Configuration() *_cache.Memory {
9595
return nil
9696
}

pkg/cache/strategy/memory/memory_test.go pkg/cache/memorycache/provider_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package memory
1+
package memorycache
22

33
import (
44
"context"

pkg/cache/strategy/redis/redis.go pkg/cache/rediscache/provider.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package redis
1+
package rediscache
22

33
import (
44
"context"
@@ -11,22 +11,22 @@ import (
1111
"go.uber.org/zap"
1212
)
1313

14-
type cache struct {
14+
type provider struct {
1515
client *redis.Client
1616
opts *_cache.Redis
1717
}
1818

19-
func New(opts *_cache.Redis) *cache {
20-
return &cache{opts: opts}
19+
func New(opts *_cache.Redis) *provider {
20+
return &provider{opts: opts}
2121
}
2222

2323
// WithClient creates a new cache with the given client
24-
func WithClient(client *redis.Client) *cache {
25-
return &cache{client: client}
24+
func WithClient(client *redis.Client) *provider {
25+
return &provider{client: client}
2626
}
2727

2828
// Connect connects to the redis server
29-
func (c *cache) Connect(_ context.Context) error {
29+
func (c *provider) Connect(_ context.Context) error {
3030
c.client = redis.NewClient(&redis.Options{
3131
Addr: fmt.Sprintf("%s:%d", c.opts.Host, c.opts.Port),
3232
Password: c.opts.Password,
@@ -36,12 +36,12 @@ func (c *cache) Connect(_ context.Context) error {
3636
}
3737

3838
// Store stores the data in the cache
39-
func (c *cache) Store(ctx context.Context, cacheKey string, data _cache.CacheableEntity, ttl time.Duration) error {
39+
func (c *provider) Store(ctx context.Context, cacheKey string, data _cache.CacheableEntity, ttl time.Duration) error {
4040
return c.client.Set(ctx, cacheKey, data, ttl).Err()
4141
}
4242

4343
// Retrieve retrieves the data from the cache
44-
func (c *cache) Retrieve(ctx context.Context, cacheKey string, dest _cache.CacheableEntity, allowExpired bool) (_cache.RetrieveStatus, error) {
44+
func (c *provider) Retrieve(ctx context.Context, cacheKey string, dest _cache.CacheableEntity, allowExpired bool) (_cache.RetrieveStatus, error) {
4545
err := c.client.Get(ctx, cacheKey).Scan(dest)
4646
if err != nil {
4747
if errors.Is(err, redis.Nil) {
@@ -53,47 +53,47 @@ func (c *cache) Retrieve(ctx context.Context, cacheKey string, dest _cache.Cache
5353
}
5454

5555
// SetTTL sets the TTL for the cache entry
56-
func (c *cache) SetTTL(ctx context.Context, cacheKey string, ttl time.Duration) {
56+
func (c *provider) SetTTL(ctx context.Context, cacheKey string, ttl time.Duration) {
5757
err := c.client.Expire(ctx, cacheKey, ttl).Err()
5858
if err != nil {
5959
zap.L().Error("error setting TTL for cache key", zap.String("cacheKey", cacheKey), zap.Duration("ttl", ttl), zap.Error(err))
6060
}
6161
}
6262

6363
// Remove removes the cache entry
64-
func (c *cache) Remove(ctx context.Context, cacheKey string) {
64+
func (c *provider) Remove(ctx context.Context, cacheKey string) {
6565
c.BulkRemove(ctx, []string{cacheKey})
6666
}
6767

6868
// BulkRemove removes the cache entries
69-
func (c *cache) BulkRemove(ctx context.Context, cacheKeys []string) {
69+
func (c *provider) BulkRemove(ctx context.Context, cacheKeys []string) {
7070
if err := c.client.Del(ctx, cacheKeys...).Err(); err != nil {
7171
zap.L().Error("error deleting cache keys", zap.Strings("cacheKeys", cacheKeys), zap.Error(err))
7272
}
7373
}
7474

7575
// Close closes the connection to the redis server
76-
func (c *cache) Close(_ context.Context) error {
76+
func (c *provider) Close(_ context.Context) error {
7777
return c.client.Close()
7878
}
7979

8080
// Ping pings the redis server
81-
func (c *cache) Ping(ctx context.Context) error {
81+
func (c *provider) Ping(ctx context.Context) error {
8282
return c.client.Ping(ctx).Err()
8383
}
8484

8585
// GetClient returns the redis client
86-
func (c *cache) GetClient() *redis.Client {
86+
func (c *provider) GetClient() *redis.Client {
8787
return c.client
8888
}
8989

9090
// GetOptions returns the options
91-
func (c *cache) GetOptions() *_cache.Redis {
91+
func (c *provider) GetOptions() *_cache.Redis {
9292
return c.opts
9393
}
9494

9595
// GetTTL returns the TTL for the cache entry
96-
func (c *cache) GetTTL(ctx context.Context, cacheKey string) time.Duration {
96+
func (c *provider) GetTTL(ctx context.Context, cacheKey string) time.Duration {
9797
ttl, err := c.client.TTL(ctx, cacheKey).Result()
9898
if err != nil {
9999
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 {
102102
}
103103

104104
// GetKeys returns the keys matching the pattern
105-
func (c *cache) GetKeys(ctx context.Context, pattern string) ([]string, error) {
105+
func (c *provider) GetKeys(ctx context.Context, pattern string) ([]string, error) {
106106
return c.client.Keys(ctx, pattern).Result()
107107
}
108108

109109
// GetKeysWithTTL returns the keys matching the pattern with their TTL
110-
func (c *cache) GetKeysWithTTL(ctx context.Context, pattern string) (map[string]time.Duration, error) {
110+
func (c *provider) GetKeysWithTTL(ctx context.Context, pattern string) (map[string]time.Duration, error) {
111111
keys, err := c.GetKeys(ctx, pattern)
112112
if err != nil {
113113
return nil, err

pkg/cache/strategy/redis/redis_test.go pkg/cache/rediscache/provider_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package redis
1+
package rediscache
22

33
import (
44
"context"

pkg/signoz/signoz.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package signoz
22

33
import (
44
"go.signoz.io/signoz/pkg/cache"
5-
"go.signoz.io/signoz/pkg/cache/strategy/memory"
6-
"go.signoz.io/signoz/pkg/cache/strategy/redis"
5+
"go.signoz.io/signoz/pkg/cache/memorycache"
6+
"go.signoz.io/signoz/pkg/cache/rediscache"
77
"go.signoz.io/signoz/pkg/config"
88
"go.signoz.io/signoz/pkg/web"
99
"go.uber.org/zap"
@@ -20,9 +20,9 @@ func New(config *config.Config, skipWebFrontend bool) (*SigNoz, error) {
2020
// init for the cache
2121
switch config.Cache.Provider {
2222
case "memory":
23-
cache = memory.New(&config.Cache.Memory)
23+
cache = memorycache.New(&config.Cache.Memory)
2424
case "redis":
25-
cache = redis.New(&config.Cache.Redis)
25+
cache = rediscache.New(&config.Cache.Redis)
2626
}
2727

2828
web, err := web.New(zap.L(), config.Web)

0 commit comments

Comments
 (0)