Initial commit for Cache Plugin

This commit is contained in:
Atharva Zade
2025-05-13 13:01:09 +05:30
parent cfeab57be2
commit 7d613ed495
4 changed files with 170 additions and 1 deletions

7
go.mod
View File

@@ -1,6 +1,8 @@
module github.com/beckn/beckn-onix
go 1.24
go 1.23.0
toolchain go1.23.4
require (
github.com/kr/pretty v0.3.1 // indirect
@@ -25,6 +27,8 @@ require github.com/zenazn/pkcs7pad v0.0.0-20170308005700-253a5b1f0e03
require golang.org/x/text v0.23.0 // indirect
require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.31.0 // indirect
@@ -32,6 +36,7 @@ require (
require (
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/redis/go-redis/v9 v9.8.0
github.com/rs/zerolog v1.34.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v2 v2.4.0

10
go.sum
View File

@@ -1,7 +1,15 @@
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
@@ -30,6 +38,8 @@ github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsK
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.8.0 h1:q3nRvjrlge/6UD7eTu/DSg2uYiU2mCL0G/uzBWqhicI=
github.com/redis/go-redis/v9 v9.8.0/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=

View 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()
}

View 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{}