otredis

package
v0.12.3 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 License: MIT Imports: 24 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 factoryIn 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
})
Example
c := core.New()
c.ProvideEssentials()
c.Provide(otredis.Providers())
c.Invoke(func(redisClient redis.UniversalClient) {
	pong, _ := redisClient.Ping(context.Background()).Result()
	fmt.Println(pong)
})
Output:

PONG

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCleanupCommand added in v0.12.2

func NewCleanupCommand(maker Maker, baseLogger log.Logger) *cobra.Command

NewCleanupCommand creates a new command to clean up unused redis keys.

func Providers added in v0.2.0

func Providers(opts ...ProvidersOptionFunc) di.Deps

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
	opentracing.Tracer            `optional:"true"`
Provide:
	Maker
	Factory
	redis.UniversalClient
	*collector

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 Gauges added in v0.5.0

type Gauges struct {
	Hits       metrics.Gauge
	Misses     metrics.Gauge
	Timeouts   metrics.Gauge
	TotalConns metrics.Gauge
	IdleConns  metrics.Gauge
	StaleConns metrics.Gauge
	// contains filtered or unexported fields
}

Gauges is a collection of metrics for redis connection info.

func (*Gauges) DBName added in v0.9.0

func (g *Gauges) DBName(dbName string) *Gauges

DBName sets the dbname label of redis metrics.

func (*Gauges) Observe added in v0.9.0

func (g *Gauges) Observe(stats *redis.PoolStats)

Observe records the redis pool stats. It should be called periodically.

type Maker

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

Maker is models Factory

type Module added in v0.5.0

type Module struct {
	// contains filtered or unexported fields
}

Module is the registration unit for package core. It provides redis command.

func New added in v0.5.0

func New(in ModuleIn) Module

New creates a Module.

func (Module) ProvideCommand added in v0.12.2

func (m Module) ProvideCommand(command *cobra.Command)

ProvideCommand provides redis commands.

type ModuleIn added in v0.8.0

type ModuleIn struct {
	di.In

	Maker  Maker
	Logger log.Logger
}

ModuleIn contains the input parameters needed for creating the new module.

type ProvidersOptionFunc added in v0.9.0

type ProvidersOptionFunc func(options *providersOption)

ProvidersOptionFunc is the type of functional providersOption for Providers. Use this type to change how Providers work.

func WithConfigInterceptor added in v0.9.0

func WithConfigInterceptor(interceptor RedisConfigurationInterceptor) ProvidersOptionFunc

WithConfigInterceptor instructs the Providers to accept the RedisConfigurationInterceptor so that users can change config during runtime. This can be useful when some dynamic computations on configs are required.

func WithReload added in v0.10.0

func WithReload(shouldReload bool) ProvidersOptionFunc

WithReload toggles whether the factory should reload cached instances upon OnReload event.

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 factoryIn 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.

Directories

Path Synopsis
Package mock_metrics is a generated GoMock package.
Package mock_metrics is a generated GoMock package.

Jump to

Keyboard shortcuts

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