Documentation
¶
Index ¶
Constants ¶
View Source
const ( EnvRedisAddress string = "REDIS_ADDR" EnvRedisUsername string = "REDIS_USERNAME" EnvRedisPassword string = "REDIS_PASSWORD" EnvRedisDatabase string = "REDIS_DATABASE" )
Variables ¶
View Source
var ( ErrRedisNoAddress = errors.New("no Redis address (REDIS_ADDR) found") ErrRedisAddressWrongFormat = errors.New("the address to Redis server is invalid") ErrRedisNoPassword = errors.New("no Redis address (REDIS_PASSWORD) found") ErrRedisNoDatabase = errors.New("no Redis database (REDIS_DATABASE) found") ErrRedisDatabaseWrongType = errors.New("the database number for Redis is in wrong format") )
View Source
var Module = fx.Options( fx.Provide( fx.Annotate( func(logger *zap.Logger, cfgs *red.Options, lc fx.Lifecycle) *red.Client { client := newRedis(cfgs) if client == nil { return nil } lc.Append(fx.Hook{ OnStart: func(ctx context.Context) error { var err error defer func() { select { case <-ctx.Done(): if logger != nil { logger.Info("Redis failed to connect", zap.String("address", cfgs.Addr)) } else { log.Printf("Redis failed to connect, address=%v\n", cfgs.Addr) } default: if err != nil { if logger != nil { logger.Info("Redis failed to connect", zap.String("address", cfgs.Addr)) } else { log.Printf("Redis failed to connect, address=%v\n", cfgs.Addr) } } if logger != nil { logger.Info("Redis connected", zap.String("address", cfgs.Addr)) } else { log.Printf("Redis connected, address=%v\n", cfgs.Addr) } } }() cmd := client.Ping(ctx) err = cmd.Err() return err }, OnStop: func(ctx context.Context) error { err := client.Close() if err != nil { if logger != nil { logger.Info("Redis disconnect failed", zap.Error(err)) } else { log.Printf("Redis disconnect failed err=%v\n", err.Error()) } return err } if logger != nil { logger.Info("Redis disconnected") } else { log.Println("Redis disconnected") } return nil }, }) return client }, fx.ParamTags( `optional:"true"`, ), ), ), )
Functions ¶
func NewDefaultRedisConfig ¶
func NewDefaultRedisConfig(addr string, username string, password string, database int) *red.Options
Create a new default Redis config
func ProvideRedisConfig ¶
Validate, then provide the app with a Redis config.
To use this:
var Module = fx.Options( qsredis.ProvideRedisConfig(&qsredis.EnvConfigType{}) )
this code will provide a new default instance of a redis.Options that can be used to instantiate a redis.Client.
in case you want to customize a redis.Options yourself, implements:
config.ConfigType[*red.Options]
If you need custom dependencies, don't use this, instead, create a fx.Option yourself:
fx.Provide(func (dep *Dependencies) (*redis.Options, error) { // Do something return &redis.Options{ // Options here } })
Types ¶
type EnvConfigType ¶
type EnvConfigType struct{}
Requires these following environment variables:
- REDIS_ADDR: The IP/address to a Redis server
- REDIS_PASSWORD: The password to the Redis server
- REDIS_DATABASE: The database number
- REDIS_USERNAME: This is optional, can be empty
Click to show internal directories.
Click to hide internal directories.