mredis

package
v0.1.19 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultName is the default group name for redis instance.
	DefaultName = "default"
)

Variables

This section is empty.

Functions

func RemoveConfig

func RemoveConfig(name ...string)

RemoveConfig removes the redis configuration with the specified name.

func SetConfig

func SetConfig(name string, cfg *Config)

SetConfig sets the redis configuration with the specified name.

func SetConfigByMap

func SetConfigByMap(m map[string]any, name ...string) error

SetConfigByMap sets the redis configuration with the specified name.

Types

type Config

type Config struct {
	// Address is the address of the Redis server.
	Address string `mconv:"address"`
	// DB is the database number.
	DB int `mconv:"db"`
	// User is the user of the Redis server.
	User string `mconv:"user"`
	// Password is the password of the Redis server.
	Password string `mconv:"password"`
	// MasterName is the master name of the Redis server.
	MasterName string `mconv:"master_name"`
	// MinIdleConns is the minimum number of idle connections.
	MinIdleConns int `mconv:"min_idle_conns"`
	// MaxIdleConns is the maximum number of idle connections.
	MaxIdleConns int `mconv:"max_idle_conns"`
	// MaxRetries is the maximum number of retries before giving up.
	MaxRetries int `mconv:"max_retries"`
	// PoolSize is the maximum number of socket connections.
	PoolSize int `mconv:"pool_size"`
	// MinRetryBackoff is the minimum backoff between each retry.
	MinRetryBackoff time.Duration `mconv:"min_retry_backoff"`
	// MaxRetryBackoff is the maximum backoff between each retry.
	MaxRetryBackoff time.Duration `mconv:"max_retry_backoff"`
	// DialTimeout is the timeout for establishing new connections.
	DialTimeout time.Duration `mconv:"dial_timeout"`
	// ReadTimeout is the timeout for reading.
	ReadTimeout time.Duration `mconv:"read_timeout"`
	// WriteTimeout is the timeout for writing.
	WriteTimeout time.Duration `mconv:"write_timeout"`
	// PoolTimeout is the timeout for getting a connection from the pool.
	PoolTimeout time.Duration `mconv:"pool_timeout"`
	// ConnMaxIdleTime is the timeout for idle connections.
	ConnMaxIdleTime time.Duration `mconv:"conn_max_idle_time"`
	// SlowThreshold is the slow threshold for the Redis.
	SlowThreshold time.Duration `mconv:"slow_threshold"`
	// Logger is the logger for the Redis.
	Logger *mlog.Logger
	// Hooks is the hooks for the Redis. It will be used to add hooks to the Redis client.
	Hooks []Hook
}

Config is the configuration object for Redis.

func ConfigFromMap

func ConfigFromMap(m map[string]any) (config *Config, err error)

ConfigFromMap parses and returns config from given map.

func GetConfig

func GetConfig(name ...string) (config *Config, ok bool)

GetConfig returns the redis configuration with the specified name. If `name` is not passed, it returns configuration of the default name.

func (*Config) AddHook added in v0.1.2

func (c *Config) AddHook(hook Hook)

func (*Config) SetConfigWithMap

func (c *Config) SetConfigWithMap(config map[string]any) error

func (*Config) SetLogger added in v0.1.2

func (c *Config) SetLogger(logger *mlog.Logger)

type Hook added in v0.1.2

type Hook redis.Hook

type Redis

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

Redis is the main struct for redis operations.

func Instance

func Instance(name ...string) *Redis

Instance returns a redis instance.

func New

func New(config ...*Config) (*Redis, error)

New creates and returns a new Redis client.

func (*Redis) AddHook added in v0.1.2

func (r *Redis) AddHook(hook Hook)

AddHook adds a hook to the client.

func (*Redis) Client

func (r *Redis) Client() redis.UniversalClient

Client returns the underlying universal client.

func (*Redis) Close

func (r *Redis) Close() error

Close closes the client, releasing any open resources.

func (*Redis) DBSize

func (r *Redis) DBSize(ctx context.Context) (int64, error)

DBSize is a redis DBSIZE command.

func (*Redis) Del

func (r *Redis) Del(ctx context.Context, keys ...string) (int64, error)

Del is a redis DEL command.

func (*Redis) Exists

func (r *Redis) Exists(ctx context.Context, keys ...string) (int64, error)

Exists is a redis EXISTS command.

func (*Redis) Expire

func (r *Redis) Expire(ctx context.Context, key string, expiration time.Duration) (bool, error)

Expire is a redis EXPIRE command.

func (*Redis) FlushDB

func (r *Redis) FlushDB(ctx context.Context) error

FlushDB is a redis FLUSHDB command.

func (*Redis) Get

func (r *Redis) Get(ctx context.Context, key string) (*mvar.Var, error)

Get retrieves and returns the associated value of given `key`. If the key does not exist the special value nil is returned. An error is returned if the value stored at key is not a string, because GET only handles string values.

func (*Redis) HGet

func (r *Redis) HGet(ctx context.Context, key, field string) (*mvar.Var, error)

HGet returns the value associated with field in the hash stored at key.

func (*Redis) HSet

func (r *Redis) HSet(ctx context.Context, key string, fields map[string]interface{}) error

HSet sets the specified fields to their respective values in the hash stored at key.

func (*Redis) Keys

func (r *Redis) Keys(ctx context.Context, pattern string) ([]string, error)

Keys is a redis KEYS command.

func (*Redis) LPush

func (r *Redis) LPush(ctx context.Context, key string, values ...interface{}) (int64, error)

LPush prepends one or multiple values to a list.

func (*Redis) MGet

func (r *Redis) MGet(ctx context.Context, keys ...string) ([]*mvar.Var, error)

MGet is a redis MGET command.

func (*Redis) MSet

func (r *Redis) MSet(ctx context.Context, data map[string]interface{}) error

MSet is a redis MSET command.

func (*Redis) Ping

func (r *Redis) Ping(ctx context.Context) error

Ping checks the connection to the server.

func (*Redis) RPop

func (r *Redis) RPop(ctx context.Context, key string) (*mvar.Var, error)

RPop removes and returns the last element of the list stored at key.

func (*Redis) SAdd

func (r *Redis) SAdd(ctx context.Context, key string, members ...interface{}) (int64, error)

SAdd adds the specified members to the set stored at key.

func (*Redis) SIsMember

func (r *Redis) SIsMember(ctx context.Context, key string, member interface{}) (bool, error)

SIsMember returns if member is a member of the set stored at key.

func (*Redis) Set

func (r *Redis) Set(ctx context.Context, key string, value interface{}) error

Set sets key to hold the string value. If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation.

func (*Redis) SetEX

func (r *Redis) SetEX(ctx context.Context, key string, value interface{}, duration time.Duration) error

SetEx sets key to hold the string value with time.

func (*Redis) SetNX

func (r *Redis) SetNX(ctx context.Context, key string, value interface{}, duration time.Duration) (bool, error)

SetNX sets key to hold string value if key does not exist. In that case, it is equal to SET. When key already holds a value, no operation is performed. SETNX is a an atomic operation.

func (*Redis) TTL

func (r *Redis) TTL(ctx context.Context, key string) (time.Duration, error)

TTL is a redis TTL command.

func (*Redis) ZAdd

func (r *Redis) ZAdd(ctx context.Context, key string, members ...Z) (int64, error)

ZAdd adds all the specified members with the specified scores to the sorted set stored at key.

func (*Redis) ZScore

func (r *Redis) ZScore(ctx context.Context, key, member string) (float64, error)

ZScore returns the score of member in the sorted set at key.

type Z

type Z = redis.Z

Jump to

Keyboard shortcuts

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