Added logs, comments and issue fix
Fixed issue for the interface not implemented for the provider while loading the plugin
This commit is contained in:
17
pkg/plugin/implementation/cache/cache.go
vendored
17
pkg/plugin/implementation/cache/cache.go
vendored
@@ -7,10 +7,11 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/beckn/beckn-onix/pkg/log"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// Global variable for the Redis client, can be overridden in tests
|
||||
// RedisCl global variable for the Redis client, can be overridden in tests
|
||||
var RedisCl *redis.Client
|
||||
|
||||
// RedisClient is an interface for Redis operations that allows mocking
|
||||
@@ -23,14 +24,17 @@ type RedisClient interface {
|
||||
Close() error
|
||||
}
|
||||
|
||||
// Config holds the configuration required to connect to Redis.
|
||||
type Config struct {
|
||||
Addr string
|
||||
}
|
||||
|
||||
// Cache wraps a Redis client to provide basic caching operations.
|
||||
type Cache struct {
|
||||
Client RedisClient
|
||||
}
|
||||
|
||||
// Error variables to describe common failure modes.
|
||||
var (
|
||||
ErrEmptyConfig = errors.New("empty config")
|
||||
ErrAddrMissing = errors.New("missing required field 'Addr'")
|
||||
@@ -38,6 +42,7 @@ var (
|
||||
ErrConnectionFail = errors.New("failed to connect to Redis")
|
||||
)
|
||||
|
||||
// validate checks if the provided Redis configuration is valid.
|
||||
func validate(cfg *Config) error {
|
||||
if cfg == nil {
|
||||
return ErrEmptyConfig
|
||||
@@ -48,6 +53,8 @@ func validate(cfg *Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RedisClientFunc is a function variable that creates a Redis client based on the provided configuration.
|
||||
// It can be overridden for testing purposes.
|
||||
var RedisClientFunc = func(cfg *Config) RedisClient {
|
||||
return redis.NewClient(&redis.Options{
|
||||
Addr: cfg.Addr,
|
||||
@@ -56,7 +63,9 @@ var RedisClientFunc = func(cfg *Config) RedisClient {
|
||||
})
|
||||
}
|
||||
|
||||
// New initializes and returns a Cache instance along with a close function to release resources.
|
||||
func New(ctx context.Context, cfg *Config) (*Cache, func() error, error) {
|
||||
log.Debugf(ctx, "Initializing Cache with config: %+v", cfg)
|
||||
if err := validate(cfg); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -64,24 +73,30 @@ func New(ctx context.Context, cfg *Config) (*Cache, func() error, error) {
|
||||
client := RedisClientFunc(cfg)
|
||||
|
||||
if _, err := client.Ping(ctx).Result(); err != nil {
|
||||
log.Errorf(ctx, err, "Failed to ping Redis server")
|
||||
return nil, nil, fmt.Errorf("%w: %v", ErrConnectionFail, err)
|
||||
}
|
||||
|
||||
log.Infof(ctx, "Cache connection to Redis established successfully")
|
||||
return &Cache{Client: client}, client.Close, nil
|
||||
}
|
||||
|
||||
// Get retrieves the value for the specified key from Redis.
|
||||
func (c *Cache) Get(ctx context.Context, key string) (string, error) {
|
||||
return c.Client.Get(ctx, key).Result()
|
||||
}
|
||||
|
||||
// Set stores the given key-value pair in Redis with the specified TTL (time to live).
|
||||
func (c *Cache) Set(ctx context.Context, key, value string, ttl time.Duration) error {
|
||||
return c.Client.Set(ctx, key, value, ttl).Err()
|
||||
}
|
||||
|
||||
// Delete removes the specified key from Redis.
|
||||
func (c *Cache) Delete(ctx context.Context, key string) error {
|
||||
return c.Client.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
// Clear removes all keys in the currently selected Redis database.
|
||||
func (c *Cache) Clear(ctx context.Context) error {
|
||||
return c.Client.FlushDB(ctx).Err()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user