diff --git a/docs/quick-start/connecting-redis/page.md b/docs/quick-start/connecting-redis/page.md index 3e002bdf5..7bae564e4 100644 --- a/docs/quick-start/connecting-redis/page.md +++ b/docs/quick-start/connecting-redis/page.md @@ -37,6 +37,7 @@ Following configuration keys are required for Redis connectivity: * `REDIS_PORT`: It specifies the port number on which your Redis server is listening. The default Redis port is 6379. * `REDIS_USER` : This is the user you'll use to connect to your Redis server. You can configure multiple users with different permissions in a single Redis container. For more details, refer to the [official docs](https://redis.io/docs/latest/operate/oss_and_stack/management/security/acl/) * `REDIS_PASSWORD`: The password is required only if your Redis server is configured for authentication; if authentication is not enabled, no password is necessary. +* `REDIS_DB`: The database number to use for the Redis server. The default value is 0. ```dotenv APP_NAME=test-service HTTP_PORT=9000 @@ -44,6 +45,7 @@ HTTP_PORT=9000 REDIS_HOST=localhost REDIS_PORT=6379 REDIS_PASSWORD=password +REDIS_DB=2 ``` The following code snippet demonstrates how to retrieve data from a Redis key named "greeting": diff --git a/docs/references/configs/page.md b/docs/references/configs/page.md index 977791d8b..e135d1555 100644 --- a/docs/references/configs/page.md +++ b/docs/references/configs/page.md @@ -215,6 +215,11 @@ This document lists all the configuration options supported by the GoFr framewor - REDIS_PASSWORD - Password for the Redis server. +--- + +- REDIS_DB +- Database number to use for the Redis server. + {% /table %} ### Pub/Sub diff --git a/pkg/gofr/datasource/redis/redis.go b/pkg/gofr/datasource/redis/redis.go index edcb9c6d5..c157a899f 100644 --- a/pkg/gofr/datasource/redis/redis.go +++ b/pkg/gofr/datasource/redis/redis.go @@ -23,6 +23,7 @@ type Config struct { Username string Password string Port int + DB int Options *redis.Options } @@ -42,7 +43,7 @@ func NewClient(c config.Config, logger datasource.Logger, metrics Metrics) *Redi return nil } - logger.Debugf("connecting to redis at '%s:%d'", redisConfig.HostName, redisConfig.Port) + logger.Debugf("connecting to redis at '%s:%d' on database %d", redisConfig.HostName, redisConfig.Port, redisConfig.DB) rc := redis.NewClient(redisConfig.Options) rc.AddHook(&redisHook{config: redisConfig, logger: logger, metrics: metrics}) @@ -55,9 +56,9 @@ func NewClient(c config.Config, logger datasource.Logger, metrics Metrics) *Redi logger.Errorf("could not add tracing instrumentation, error: %s", err) } - logger.Infof("connected to redis at %s:%d", redisConfig.HostName, redisConfig.Port) + logger.Infof("connected to redis at %s:%d on database %d", redisConfig.HostName, redisConfig.Port, redisConfig.DB) } else { - logger.Errorf("could not connect to redis at '%s:%d', error: %s", redisConfig.HostName, redisConfig.Port, err) + logger.Errorf("could not connect to redis at '%s:%d' , error: %s", redisConfig.HostName, redisConfig.Port, err) } return &Redis{Client: rc, config: redisConfig, logger: logger} @@ -88,6 +89,13 @@ func getRedisConfig(c config.Config) *Config { redisConfig.Port = port + db, err := strconv.Atoi(c.Get("REDIS_DB")) + if err != nil { + db = 0 // default to DB 0 if not specified + } + + redisConfig.DB = db + options := new(redis.Options) if options.Addr == "" { @@ -102,6 +110,8 @@ func getRedisConfig(c config.Config) *Config { options.Password = redisConfig.Password } + options.DB = redisConfig.DB + redisConfig.Options = options return redisConfig diff --git a/pkg/gofr/datasource/redis/redis_test.go b/pkg/gofr/datasource/redis/redis_test.go index 71746eb22..0da3ea2f5 100644 --- a/pkg/gofr/datasource/redis/redis_test.go +++ b/pkg/gofr/datasource/redis/redis_test.go @@ -60,6 +60,7 @@ func TestRedis_QueryLogging(t *testing.T) { client := NewClient(config.NewMockConfig(map[string]string{ "REDIS_HOST": s.Host(), "REDIS_PORT": s.Port(), + "REDIS_DB": "1", }), mockLogger, mockMetric) require.NoError(t, err)