From 014f5741835e28e6309e56db0f57a492501c3202 Mon Sep 17 00:00:00 2001 From: pushkarajs <pushkarajs@mkcl.org> Date: Tue, 28 May 2024 13:01:42 +0530 Subject: [PATCH] redis authentication. added username functionality in cache_redis.go --- cachemdl/cache_redis.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/cachemdl/cache_redis.go b/cachemdl/cache_redis.go index 416b900..cc37960 100644 --- a/cachemdl/cache_redis.go +++ b/cachemdl/cache_redis.go @@ -40,16 +40,18 @@ type RedisCache struct { addPrefix bool // connected bool // will be enabled if redis client connects to server - Addr string // redis server address, default "127.0.0.1:6379" - DB int // redis DB on provided server, default 0 + Addr string // redis server address, default "127.0.0.1:6379" + DB int // redis DB on provided server, default 0 + Username string Password string // Expiration time.Duration // this duration will be used for Set() method Prefix string // this will be used for storing keys for provided project } type configRedis struct { - addr string // redis server address, default "127.0.0.1:6379" - db int // redis DB on provided server, default 0 + addr string // redis server address, default "127.0.0.1:6379" + db int // redis DB on provided server, default 0 + username string password string // expiration time.Duration // this duration will be used for Set() method prefix string // this will be used for storing keys for provided project @@ -82,9 +84,14 @@ func RedisWithExpiration(exp time.Duration) redisOption { cfg.expiration = exp } } +func RedisWithUsername(username string) redisOption { + return func(cfg *configRedis) { + cfg.username = username + } +} // Setup initializes redis cache for application. Must be called only once. -func (rc *RedisCache) Setup(addr, password, prefix string, db int, exp time.Duration) { +func (rc *RedisCache) Setup(addr, username, password, prefix string, db int, exp time.Duration) { if rc == nil { rc = new(RedisCache) @@ -92,6 +99,7 @@ func (rc *RedisCache) Setup(addr, password, prefix string, db int, exp time.Dura rc.Addr = addr rc.Password = password + rc.Username = username rc.DB = db rc.Expiration = exp rc.Prefix = prefix @@ -99,6 +107,7 @@ func (rc *RedisCache) Setup(addr, password, prefix string, db int, exp time.Dura Addr: addr, Password: password, DB: db, + Username: username, } rc.opt = &opt @@ -132,6 +141,7 @@ func SetupRedisCache(opts ...redisOption) (*RedisCache, error) { rc.Addr = cfg.addr rc.Password = cfg.password + rc.Username = cfg.username rc.DB = cfg.db rc.Expiration = cfg.expiration rc.Prefix = cfg.prefix @@ -140,6 +150,7 @@ func SetupRedisCache(opts ...redisOption) (*RedisCache, error) { Addr: cfg.addr, Password: cfg.password, DB: cfg.db, + Username: cfg.username, } rc.cli = redis.NewClient(rc.opt) -- GitLab