Skip to content

Commit

Permalink
Support to change default redisDB (#1318)
Browse files Browse the repository at this point in the history
  • Loading branch information
Umang01-hash authored Dec 18, 2024
1 parent e542467 commit 0a8048d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/quick-start/connecting-redis/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ 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
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":
Expand Down
5 changes: 5 additions & 0 deletions docs/references/configs/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions pkg/gofr/datasource/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Config struct {
Username string
Password string
Port int
DB int
Options *redis.Options
}

Expand All @@ -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})
Expand All @@ -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}
Expand Down Expand Up @@ -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 == "" {
Expand All @@ -102,6 +110,8 @@ func getRedisConfig(c config.Config) *Config {
options.Password = redisConfig.Password
}

options.DB = redisConfig.DB

redisConfig.Options = options

return redisConfig
Expand Down
1 change: 1 addition & 0 deletions pkg/gofr/datasource/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0a8048d

Please sign in to comment.