Initial commit for Cache Plugin
This commit is contained in:
77
pkg/plugin/implementation/cache/cache.go
vendored
Normal file
77
pkg/plugin/implementation/cache/cache.go
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Addr string
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
var (
|
||||
ErrEmptyConfig = errors.New("empty config")
|
||||
ErrAddrMissing = errors.New("missing required field 'Addr'")
|
||||
ErrCredentialMissing = errors.New("missing Redis credentials in environment")
|
||||
ErrConnectionFail = errors.New("failed to connect to Redis")
|
||||
)
|
||||
|
||||
func validate(cfg *Config) error {
|
||||
if cfg == nil {
|
||||
return ErrEmptyConfig
|
||||
}
|
||||
if cfg.Addr == "" {
|
||||
return ErrAddrMissing
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func New(ctx context.Context, cfg *Config) (*Cache, func() error, error) {
|
||||
if err := validate(cfg); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// Get password from environment variable
|
||||
password := os.Getenv("REDIS_PASSWORD")
|
||||
// Allow empty password for local testing
|
||||
// if password == "" {
|
||||
// return nil, nil, ErrCredentialMissing
|
||||
// }
|
||||
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: cfg.Addr,
|
||||
Password: password,
|
||||
DB: 0, // Always use default DB 0
|
||||
})
|
||||
|
||||
if _, err := client.Ping(ctx).Result(); err != nil {
|
||||
return nil, nil, fmt.Errorf("%w: %v", ErrConnectionFail, err)
|
||||
}
|
||||
|
||||
return &Cache{client: client}, client.Close, nil
|
||||
}
|
||||
|
||||
func (c *Cache) Get(ctx context.Context, key string) (string, error) {
|
||||
return c.client.Get(ctx, key).Result()
|
||||
}
|
||||
|
||||
func (c *Cache) Set(ctx context.Context, key, value string, ttl time.Duration) error {
|
||||
return c.client.Set(ctx, key, value, ttl).Err()
|
||||
}
|
||||
|
||||
func (c *Cache) Delete(ctx context.Context, key string) error {
|
||||
return c.client.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
func (c *Cache) Clear(ctx context.Context) error {
|
||||
return c.client.FlushDB(ctx).Err()
|
||||
}
|
||||
77
pkg/plugin/implementation/cache/cmd/plugin.go
vendored
Normal file
77
pkg/plugin/implementation/cache/cmd/plugin.go
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/beckn/beckn-onix/pkg/plugin/definition"
|
||||
"github.com/beckn/beckn-onix/pkg/plugin/implementation/cache"
|
||||
)
|
||||
|
||||
// cacheProvider implements the definition.CacheProvider interface.
|
||||
type cacheProvider struct{}
|
||||
|
||||
// Config holds the configuration settings for the Redis cache plugin.
|
||||
type Config struct {
|
||||
Addr string // Redis address (host:port)
|
||||
DB int // Redis database number (optional, defaults to 0)
|
||||
Password string // Redis password (optional, can be empty or from env)
|
||||
}
|
||||
|
||||
// parseConfig converts the string map configuration to a Config struct.
|
||||
func parseConfig(config map[string]string) (*Config, error) {
|
||||
addr, ok := config["addr"]
|
||||
if !ok || addr == "" {
|
||||
return nil, errors.New("config must contain 'addr'")
|
||||
}
|
||||
|
||||
// Default values
|
||||
db := 0
|
||||
password := ""
|
||||
|
||||
// Parse DB if provided
|
||||
if val, ok := config["db"]; ok && val != "" {
|
||||
if parsedVal, err := strconv.Atoi(val); err == nil {
|
||||
db = parsedVal
|
||||
}
|
||||
}
|
||||
|
||||
// Get password if provided
|
||||
if val, ok := config["password"]; ok {
|
||||
password = val
|
||||
}
|
||||
|
||||
return &Config{
|
||||
Addr: addr,
|
||||
DB: db,
|
||||
Password: password,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// convertToRedisConfig converts the plugin Config to redis.Config.
|
||||
func convertToRedisConfig(cfg *Config) *cache.Config {
|
||||
return &cache.Config{
|
||||
Addr: cfg.Addr,
|
||||
}
|
||||
}
|
||||
|
||||
// New initializes a new Redis cache with the given configuration.
|
||||
func (p cacheProvider) New(ctx context.Context, config map[string]string) (definition.Cache, func() error, error) {
|
||||
if ctx == nil {
|
||||
return nil, nil, errors.New("context cannot be nil")
|
||||
}
|
||||
|
||||
cfg, err := parseConfig(config)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// Convert to redis.Config
|
||||
redisConfig := convertToRedisConfig(cfg)
|
||||
|
||||
return cache.New(ctx, redisConfig)
|
||||
}
|
||||
|
||||
// Provider is the exported symbol that the plugin manager will look for.
|
||||
var Provider = cacheProvider{}
|
||||
Reference in New Issue
Block a user