2025-01-17 18:09:39 +05:30
|
|
|
package rediscache
|
2025-01-04 01:28:54 +05:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-05-03 18:30:07 +05:30
|
|
|
"strings"
|
2025-01-04 01:28:54 +05:30
|
|
|
"time"
|
|
|
|
|
|
2025-05-03 18:30:07 +05:30
|
|
|
"fmt"
|
|
|
|
|
|
2025-03-20 21:01:41 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/cache"
|
2025-07-05 19:08:23 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/errors"
|
2025-03-20 21:01:41 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/factory"
|
2025-05-03 18:30:07 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/types/cachetypes"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/valuer"
|
2025-01-04 01:28:54 +05:30
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
|
)
|
|
|
|
|
|
2025-01-17 18:09:39 +05:30
|
|
|
type provider struct {
|
2025-05-25 11:40:39 +05:30
|
|
|
client *redis.Client
|
|
|
|
|
settings factory.ScopedProviderSettings
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-01-20 17:45:33 +05:30
|
|
|
func NewFactory() factory.ProviderFactory[cache.Cache, cache.Config] {
|
|
|
|
|
return factory.NewProviderFactory(factory.MustNewName("redis"), New)
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-25 11:40:39 +05:30
|
|
|
func New(ctx context.Context, providerSettings factory.ProviderSettings, config cache.Config) (cache.Cache, error) {
|
|
|
|
|
settings := factory.NewScopedProviderSettings(providerSettings, "github.com/SigNoz/signoz/pkg/cache/rediscache")
|
|
|
|
|
client := redis.NewClient(&redis.Options{
|
2025-05-03 18:30:07 +05:30
|
|
|
Addr: strings.Join([]string{config.Redis.Host, fmt.Sprint(config.Redis.Port)}, ":"),
|
|
|
|
|
Password: config.Redis.Password,
|
|
|
|
|
DB: config.Redis.DB,
|
|
|
|
|
})
|
|
|
|
|
|
2025-05-25 11:40:39 +05:30
|
|
|
if err := client.Ping(ctx).Err(); err != nil {
|
2025-05-03 18:30:07 +05:30
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-25 11:40:39 +05:30
|
|
|
return &provider{client: client, settings: settings}, nil
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-05-03 18:30:07 +05:30
|
|
|
func (c *provider) Set(ctx context.Context, orgID valuer.UUID, cacheKey string, data cachetypes.Cacheable, ttl time.Duration) error {
|
|
|
|
|
return c.client.Set(ctx, strings.Join([]string{orgID.StringValue(), cacheKey}, "::"), data, ttl).Err()
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-05-03 18:30:07 +05:30
|
|
|
func (c *provider) Get(ctx context.Context, orgID valuer.UUID, cacheKey string, dest cachetypes.Cacheable, allowExpired bool) error {
|
|
|
|
|
err := c.client.Get(ctx, strings.Join([]string{orgID.StringValue(), cacheKey}, "::")).Scan(dest)
|
2025-01-04 01:28:54 +05:30
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, redis.Nil) {
|
2025-07-05 19:08:23 +05:30
|
|
|
return errors.Newf(errors.TypeNotFound, errors.CodeNotFound, "key miss")
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
2025-07-05 19:08:23 +05:30
|
|
|
|
2025-05-03 18:30:07 +05:30
|
|
|
return err
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
2025-07-05 19:08:23 +05:30
|
|
|
|
2025-05-03 18:30:07 +05:30
|
|
|
return nil
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-05-03 18:30:07 +05:30
|
|
|
func (c *provider) Delete(ctx context.Context, orgID valuer.UUID, cacheKey string) {
|
|
|
|
|
c.DeleteMany(ctx, orgID, []string{cacheKey})
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-05-03 18:30:07 +05:30
|
|
|
func (c *provider) DeleteMany(ctx context.Context, orgID valuer.UUID, cacheKeys []string) {
|
|
|
|
|
updatedCacheKeys := []string{}
|
|
|
|
|
for _, cacheKey := range cacheKeys {
|
|
|
|
|
updatedCacheKeys = append(updatedCacheKeys, strings.Join([]string{orgID.StringValue(), cacheKey}, "::"))
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
|
2025-05-03 18:30:07 +05:30
|
|
|
if err := c.client.Del(ctx, updatedCacheKeys...).Err(); err != nil {
|
2025-05-25 11:40:39 +05:30
|
|
|
c.settings.Logger().ErrorContext(ctx, "error deleting cache keys", "cache_keys", cacheKeys, "error", err)
|
2025-01-04 01:28:54 +05:30
|
|
|
}
|
|
|
|
|
}
|