redis

package
v2.5.4 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 7 Imported by: 6

README

Redis

Redis provider implementation.

Better encoder:

  • Encode: session.MSGPEncode
  • Decode: session.MSGPDecode

Clients

The redis provider supports the upstream standard client via New(), the sentinel client via NewFailover(), and the sentinel fail over client via NewFailoverCluster().

The difference between the standard client and the sentinel clients is that the standard client will only connect to a single redis server and the sentinel clients will connect to sentinel servers and figure out which redis server to connect to automatically. This allows for a failure of a redis server if you configure sentinel and redis correctly.

The difference between the sentinel client via NewFailover() and the sentinel fail over client via NewFailoverCluster() is the fail over client will fail over to other sentinels if they are configured in the event of a failure.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConfigAddrEmpty       = errors.New("Config Addr must not be empty")
	ErrConfigMasterNameEmpty = errors.New("Config MasterName must not be empty")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// Key prefix
	KeyPrefix string

	// Pointer to the logger interface.
	Logger Logger

	// The network type, either tcp or unix.
	// Default is tcp.
	Network string

	// host:port address.
	Addr string

	// Optional username.
	Username string

	// Optional password. Must match the password specified in the
	// requirepass server configuration option.
	Password string

	// Database to be selected after connecting to the server.
	DB int

	// Maximum number of retries before giving up.
	// Default is to not retry failed commands.
	MaxRetries int

	// Minimum backoff between each retry.
	// Default is 8 milliseconds; -1 disables backoff.
	MinRetryBackoff time.Duration

	// Maximum backoff between each retry.
	// Default is 512 milliseconds; -1 disables backoff.
	MaxRetryBackoff time.Duration

	// Dial timeout for establishing new connections.
	// Default is 5 seconds.
	DialTimeout time.Duration

	// Timeout for socket reads. If reached, commands will fail
	// with a timeout instead of blocking. Use value -1 for no timeout and 0 for default.
	// Default is 3 seconds.
	ReadTimeout time.Duration

	// Timeout for socket writes. If reached, commands will fail
	// with a timeout instead of blocking.
	// Default is ReadTimeout.
	WriteTimeout time.Duration

	// Maximum number of socket connections.
	// Default is 10 connections per every CPU as reported by runtime.NumCPU.
	PoolSize int

	// Minimum number of idle connections which is useful when establishing
	// new connection is slow.
	MinIdleConns int

	// Maximum number of idle connections.
	MaxIdleConns int

	// Deprecated: This field has been renamed to ConnMaxLifetime
	MaxConnAge time.Duration

	// Maximum amount of time a connection may be reused.
	// Expired connections may be closed lazily before reuse.
	// If <= 0, connections are not closed due to a connection's age.
	// Default is to not close idle connections.
	ConnMaxLifetime time.Duration

	// Amount of time client waits for connection if all connections
	// are busy before returning an error.
	// Default is ReadTimeout + 1 second.
	PoolTimeout time.Duration

	// Deprecated: This field has been renamed to ConnMaxIdleTime
	IdleTimeout time.Duration

	// Deprecated: This field has been removed in favor of MaxIdleConns
	IdleCheckFrequency time.Duration

	// Maximum amount of time a connection may be idle.
	// Should be less than server's timeout.
	// Expired connections may be closed lazily before reuse.
	// If d <= 0, connections are not closed due to a connection's idle time.
	// Default is 30 minutes. -1 disables idle timeout check.
	ConnMaxIdleTime time.Duration

	// TLS Config to use. When set TLS will be negotiated.
	TLSConfig *tls.Config

	// Limiter interface used to implemented circuit breaker or rate limiter.
	Limiter redis.Limiter
}

Config provider settings

type FailoverConfig added in v2.3.0

type FailoverConfig struct {
	// Key prefix
	KeyPrefix string

	// Pointer to the logger interface.
	Logger Logger

	// Optional username.
	Username string

	// Optional password. Must match the password specified in the
	// requirepass server configuration option.
	Password string

	// Database to be selected after connecting to the server.
	DB int

	// The sentinel master name.
	MasterName string

	// The sentinel nodes seed list (host:port).
	SentinelAddrs []string

	// The username to use for the sentinel connection if required. If specified, the Redis
	// client will attempt to authenticate via ACL authentication. If not specified, the
	// client will use requirepass-style authentication.
	SentinelUsername string

	// The password for the sentinel connection if required (different to username/password).
	SentinelPassword string

	// Routes read-only commands to the closest node. Only relevant with NewFailoverCluster.
	RouteByLatency bool

	// Routes read-only commands in random order. Only relevant with NewFailoverCluster.
	RouteRandomly bool

	// Deprecated: This field has been renamed to ReplicaOnly
	SlaveOnly bool

	// Route all commands to replica read-only nodes.
	ReplicaOnly bool

	// Maximum number of retries before giving up.
	// Default is to not retry failed commands.
	MaxRetries int

	// Minimum backoff between each retry.
	// Default is 8 milliseconds; -1 disables backoff.
	MinRetryBackoff time.Duration

	// Maximum backoff between each retry.
	// Default is 512 milliseconds; -1 disables backoff.
	MaxRetryBackoff time.Duration

	// Dial timeout for establishing new connections.
	// Default is 5 seconds.
	DialTimeout time.Duration

	// Timeout for socket reads. If reached, commands will fail
	// with a timeout instead of blocking. Use value -1 for no timeout and 0 for default.
	// Default is 3 seconds.
	ReadTimeout time.Duration

	// Timeout for socket writes. If reached, commands will fail
	// with a timeout instead of blocking.
	// Default is ReadTimeout.
	WriteTimeout time.Duration

	// Maximum number of socket connections.
	// Default is 10 connections per every CPU as reported by runtime.NumCPU.
	PoolSize int

	// Minimum number of idle connections which is useful when establishing
	// new connection is slow.
	MinIdleConns int

	// Maximum number of idle connections.
	MaxIdleConns int

	// Deprecated: This field has been renamed to ConnMaxLifetime
	MaxConnAge time.Duration

	// Maximum amount of time a connection may be reused.
	// Expired connections may be closed lazily before reuse.
	// If <= 0, connections are not closed due to a connection's age.
	// Default is to not close idle connections.
	ConnMaxLifetime time.Duration

	// Amount of time client waits for connection if all connections
	// are busy before returning an error.
	// Default is ReadTimeout + 1 second.
	PoolTimeout time.Duration

	// Deprecated: This field has been renamed to ConnMaxIdleTime
	IdleTimeout time.Duration

	// Maximum amount of time a connection may be idle.
	// Should be less than server's timeout.
	// Expired connections may be closed lazily before reuse.
	// If d <= 0, connections are not closed due to a connection's idle time.
	// Default is 30 minutes. -1 disables idle timeout check.
	ConnMaxIdleTime time.Duration

	// TLS Config to use. When set TLS will be negotiated.
	TLSConfig *tls.Config
}

FailoverConfig provider settings.

type Logger added in v2.4.2

type Logger interface {
	Printf(ctx context.Context, format string, v ...interface{})
}

Logger implements the upstream redis internal Logger interface.

type Provider

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

Provider backend manager

func New

func New(cfg Config) (*Provider, error)

New returns a new configured redis provider

func NewFailover added in v2.3.0

func NewFailover(cfg FailoverConfig) (*Provider, error)

NewFailover returns a new redis provider using sentinel to determine the redis server to connect to.

func NewFailoverCluster added in v2.3.0

func NewFailoverCluster(cfg FailoverConfig) (*Provider, error)

NewFailoverCluster returns a new redis provider using a group of sentinels to determine the redis server to connect to.

func (*Provider) Count

func (p *Provider) Count() int

Count returns the total of stored sessions

func (*Provider) Destroy

func (p *Provider) Destroy(id []byte) error

Destroy destroys the session from the given id

func (*Provider) GC

func (p *Provider) GC() error

GC destroys the expired sessions

func (*Provider) Get

func (p *Provider) Get(id []byte) ([]byte, error)

Get returns the data of the given session id

func (*Provider) NeedGC

func (p *Provider) NeedGC() bool

NeedGC indicates if the GC needs to be run

func (*Provider) Regenerate

func (p *Provider) Regenerate(id, newID []byte, expiration time.Duration) error

Regenerate updates the session id and expiration with the new session id of the given current session id

func (*Provider) Save

func (p *Provider) Save(id, data []byte, expiration time.Duration) error

Save saves the session data and expiration from the given session id

Jump to

Keyboard shortcuts

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