diff --git a/cachemdl/cache_redis.go b/cachemdl/cache_redis.go index 416b900a5de0afc16e8d382cb39b4b169594575e..cc37960c2443cdf32417153ac442a584f77034f8 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)