src

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	DefaultHost         = "127.0.0.1"
	DefaultNetwork      = "tcp"
	DefaultPort         = 6379
	DefaultMaxActive    = 10
	DefaultMinIdle      = 2
	DefaultMaxLifetime  = 60
	DefaultTimeoutDial  = 5
	DefaultTimeoutRead  = 3
	DefaultTimeoutWrite = 3
)
View Source
const (
	DefaultLockDuration = time.Second * 60
	DefaultLockRenew    = 25
	DefaultLockPrefix   = "__LOCKER__"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client = gr.Client

type Cmd

type Cmd = gr.Cmd

type Config

type Config struct {
	Network  string `yaml:"network"`
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	Password string `yaml:"password"`
	Database int    `yaml:"database"`

	MaxActive   int `yaml:"max-active"`
	MaxLifetime int `yaml:"max-lifetime"`
	MinIdle     int `yaml:"min-idle"`

	DialTimeout  int `yaml:"dial-timeout"`
	ReadTimeout  int `yaml:"read-timeout"`
	WriteTimeout int `yaml:"write-timeout"`
}

Config is a component used to parse configurations.

func (*Config) After

func (o *Config) After()

After is an event handler that called by config.Resource, Do not call directly.

type Connection

type Connection = gr.Conn

type ConnectorManager

type ConnectorManager interface {
	// Do
	// send a command directly.
	Do(ctx context.Context, args ...any) (*Cmd, error)

	// GetClient
	// returns a redis client.
	GetClient() (client *Client, err error)

	// GetConfig
	// returns a config component.
	GetConfig() *Config

	// GetConnection
	// acquire a redis connection from pool.
	GetConnection(ctx context.Context) (connection *Connection, err error)

	// GetError
	// returns a error on connector.
	GetError() error
}

ConnectorManager is an interface used to manage redis connection.

var Connector ConnectorManager

Connector is a singleton instance used to manage redis connection.

type Locker

type Locker interface {
	// Release
	// applied resource.
	Release()

	// Renew
	// prevent distributed lock from being deleted due to timeout.
	Renew(seconds ...int)
}

Locker is an interface of locker.

func Lock

func Lock(ctx context.Context, key string, timeouts ...int) (Locker, error)

Lock apply a distributed lock resource. Return nil if given key applied by other connection.

Example
ctx := context.Background()
key := "key"

// Apply distributed lock resource.
res, err := Lock(ctx, key)
if err != nil {
	return
}

// Apply failed, It's applied by other goroutine.
if res == nil {
	return
}

// Release applied distributed lock resource when done.
defer res.Release()
res.Renew()

// Your logic code ...
time.Sleep(time.Second * 3)
Output:

Jump to

Keyboard shortcuts

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