fix : add the tls support for the redis cache

This commit is contained in:
Manendra Pal Singh
2026-01-09 12:40:10 +05:30
parent bbc7ff91f9
commit 58457d53c2
3 changed files with 22 additions and 4 deletions

View File

@@ -601,8 +601,19 @@ cache:
addr: 10.81.192.4:6379 addr: 10.81.192.4:6379
``` ```
**Or with TLS enabled:**
```yaml
cache:
id: cache
config:
addr: redis.example.com:6380
use_tls: "true"
```
**Parameters**: **Parameters**:
- `addr`: Redis server address and port - `addr`: Redis server address and port
- `use_tls`: Enable TLS connection to Redis (`"true"` to enable, omit or any other value to disable). Default: disabled.
--- ---

View File

@@ -2,6 +2,7 @@ package cache
import ( import (
"context" "context"
"crypto/tls"
"errors" "errors"
"fmt" "fmt"
"os" "os"
@@ -32,6 +33,7 @@ type RedisClient interface {
// Config holds the configuration required to connect to Redis. // Config holds the configuration required to connect to Redis.
type Config struct { type Config struct {
Addr string Addr string
UseTLS bool
} }
// Cache wraps a Redis client to provide basic caching operations. // Cache wraps a Redis client to provide basic caching operations.
@@ -62,11 +64,15 @@ func validate(cfg *Config) error {
// RedisClientFunc is a function variable that creates a Redis client based on the provided configuration. // RedisClientFunc is a function variable that creates a Redis client based on the provided configuration.
// It can be overridden for testing purposes. // It can be overridden for testing purposes.
var RedisClientFunc = func(cfg *Config) RedisClient { var RedisClientFunc = func(cfg *Config) RedisClient {
return redis.NewClient(&redis.Options{ opts := &redis.Options{
Addr: cfg.Addr, Addr: cfg.Addr,
Password: os.Getenv("REDIS_PASSWORD"), Password: os.Getenv("REDIS_PASSWORD"),
DB: 0, DB: 0,
}) }
if cfg.UseTLS {
opts.TLSConfig = &tls.Config{}
}
return redis.NewClient(opts)
} }
// New initializes and returns a Cache instance along with a close function to release resources. // New initializes and returns a Cache instance along with a close function to release resources.

View File

@@ -20,6 +20,7 @@ func (c cacheProvider) New(ctx context.Context, config map[string]string) (defin
// Create cache.Config directly from map - validation is handled by cache.New // Create cache.Config directly from map - validation is handled by cache.New
cacheConfig := &cache.Config{ cacheConfig := &cache.Config{
Addr: config["addr"], Addr: config["addr"],
UseTLS: config["use_tls"] == "true",
} }
log.Debugf(ctx, "Cache config mapped: %+v", cacheConfig) log.Debugf(ctx, "Cache config mapped: %+v", cacheConfig)
cache, closer, err := cache.New(ctx, cacheConfig) cache, closer, err := cache.New(ctx, cacheConfig)