mredis

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2025 License: MIT Imports: 14 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         string        `json:"address"`
	DB              int           `json:"db"`
	User            string        `json:"user"`
	Password        string        `json:"password"`
	MasterName      string        `json:"masterName"`      // sentinel master name
	MinIdleConns    int           `json:"minIdleConns"`    // minimum number of idle connections
	MaxIdleConns    int           `json:"maxIdleConns"`    // maximum number of idle connections
	MaxRetries      int           `json:"maxRetries"`      // maximum number of retries before giving up
	PoolSize        int           `json:"poolSize"`        // maximum number of socket connections
	MinRetryBackoff time.Duration `json:"minRetryBackoff"` // minimum backoff between each retry
	MaxRetryBackoff time.Duration `json:"maxRetryBackoff"` // maximum backoff between each retry
	DialTimeout     time.Duration `json:"dialTimeout"`     // timeout for establishing new connections
	ReadTimeout     time.Duration `json:"readTimeout"`     // timeout for reading
	WriteTimeout    time.Duration `json:"writeTimeout"`    // timeout for writing
	PoolTimeout     time.Duration `json:"poolTimeout"`     // timeout for getting a connection from the pool
	ConnMaxIdleTime time.Duration `json:"connMaxIdleTime"` // timeout for idle connections
	SlowThreshold   time.Duration `json:"slowThreshold"`   // slow query threshold
	Logger          *mlog.Logger  `json:"-"`
	Hooks           []Hook        `json:"-"`
}

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