otredis

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2021 License: MIT Imports: 13 Imported by: 1

Documentation

Overview

Package otredis provides redis client with opentracing. For documentation about redis usage, see https://github.com/go-redis/redis

package otredis works with redis cluster, redis sentinel and single redis instance.

Integration

package otredis exports the configuration in the following format:

redis:
    default:
        addrs:
          - 127.0.0.1:6379
        db: 0
        username: ""
        password: ""
        sentinelPassword: ""
        maxRetries: 0
        minRetryBackoff: 0s
        maxRetryBackoff: 0s
        dialTimeout: 0s
        readTimeout: 0s
        writeTimeout: 0s
        poolSize: 0
        minIdleConns: 0
        maxConnAge: 0s
        poolTimeout: 0s
        idleTimeout: 0s
        idleCheckFrequency: 0s
        maxRedirects: 0
        readOnly: false
        routeByLatency: false
        routeRandomly: false
        masterName: ""

To see all available configurations, use the config init command.

Add the redis dependency to core:

var c *core.C = core.New()
c.Provide(otredis.Providers())

Then you can invoke redis from the application.

c.Invoke(func(redisClient redis.UniversalClient) {
	redisClient.Ping(context.Background())
})

Sometimes there are valid reasons to connect to more than one redis server. Inject otredis.Maker to factory a redis.UniversalClient with a specific configuration entry.

c.Invoke(function(maker otredis.Maker) {
	client, err := maker.Make("default")
	// do something with client
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Providers added in v0.2.0

func Providers() []interface{}

Providers returns a set of dependency providers related to redis. It includes the Maker, the default redis.UniversalClient and exported configs.

Depends On:
	log.Logger
	contract.ConfigAccessor
	RedisConfigurationInterceptor `optional:"true"`
	opentracing.Tracer            `optional:"true"`
Provide:
	Maker
	Factory
	redis.UniversalClient

Types

type Factory

type Factory struct {
	*di.Factory
}

Factory is a *di.Factory that creates redis.UniversalClient using a specific configuration entry.

func (Factory) Make

func (r Factory) Make(name string) (redis.UniversalClient, error)

Make creates redis.UniversalClient using a specific configuration entry.

type Maker

type Maker interface {
	Make(name string) (redis.UniversalClient, error)
}

Maker is models Factory

type RedisConfigurationInterceptor

type RedisConfigurationInterceptor func(name string, opts *redis.UniversalOptions)

RedisConfigurationInterceptor intercepts the redis.UniversalOptions before creating the client so you can make amendment to it. Useful because some configuration can not be mapped to a text representation. For example, you cannot add OnConnect callback in a configuration file, but you can add it here.

type RedisLogAdapter added in v0.4.0

type RedisLogAdapter struct {
	Logging log.Logger
}

RedisLogAdapter is an adapter between kitlog and redis logger interface

func (RedisLogAdapter) Printf added in v0.4.0

func (r RedisLogAdapter) Printf(ctx context.Context, s string, i ...interface{})

Printf implements internal.Logging

type RedisUniversalOptions added in v0.4.1

type RedisUniversalOptions struct {
	// Either a single address or a seed list of host:port addresses
	// of cluster/sentinel nodes.
	Addrs []string `json:"addrs" yaml:"addrs"`

	// Database to be selected after connecting to the server.
	// Only single-node and failover clients.
	DB int `json:"db" yaml:"db"`

	Username         string `json:"username" yaml:"username"`
	Password         string `json:"password" yaml:"password"`
	SentinelPassword string `json:"sentinelPassword" yaml:"sentinelPassword"`

	MaxRetries      int             `json:"maxRetries" yaml:"maxRetries"`
	MinRetryBackoff config.Duration `json:"minRetryBackoff" yaml:"minRetryBackoff"`
	MaxRetryBackoff config.Duration `json:"maxRetryBackoff" yaml:"maxRetryBackoff"`

	DialTimeout  config.Duration `json:"dialTimeout" yaml:"dialTimeout"`
	ReadTimeout  config.Duration `json:"readTimeout" yaml:"readTimeout"`
	WriteTimeout config.Duration `json:"writeTimeout" yaml:"writeTimeout"`

	PoolSize           int             `json:"poolSize" yaml:"poolSize"`
	MinIdleConns       int             `json:"minIdleConns" yaml:"minIdleConns"`
	MaxConnAge         config.Duration `json:"maxConnAge" yaml:"maxConnAge"`
	PoolTimeout        config.Duration `json:"poolTimeout" yaml:"poolTimeout"`
	IdleTimeout        config.Duration `json:"idleTimeout" yaml:"idleTimeout"`
	IdleCheckFrequency config.Duration `json:"idleCheckFrequency" yaml:"idleCheckFrequency"`

	MaxRedirects   int  `json:"maxRedirects" yaml:"maxRedirects"`
	ReadOnly       bool `json:"readOnly" yaml:"readOnly"`
	RouteByLatency bool `json:"routeByLatency" yaml:"routeByLatency"`
	RouteRandomly  bool `json:"routeRandomly" yaml:"routeRandomly"`

	// The sentinel master name.
	// Only failover clients.
	MasterName string `json:"masterName" yaml:"masterName"`
}

RedisUniversalOptions is the configuration options for redis.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL