predis

package
v1.1.21 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: MIT Imports: 12 Imported by: 0

README

predis

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLockFailed  = errors.New("redis lock failed")
	ErrDuplicated  = errors.New("task duplicated")
	ErrLockNotHeld = errors.New("lock not held")
)

Functions

This section is empty.

Types

type RedisClient

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

func NewRedisClient

func NewRedisClient(pool *redis.Pool) *RedisClient

func NewRedisClientWithAddr added in v1.1.2

func NewRedisClientWithAddr(addr string, db int, maxIdle int, password ...string) *RedisClient

func (*RedisClient) Close

func (rc *RedisClient) Close() error

func (*RedisClient) Delete

func (rc *RedisClient) Delete(key string) error

func (*RedisClient) Do

func (rc *RedisClient) Do(command string, args ...any) (reply any, err error)

func (*RedisClient) DoWithTransactionPipeline

func (rc *RedisClient) DoWithTransactionPipeline(watchKey []string, commands ...[]any) error

func (*RedisClient) Get

func (rc *RedisClient) Get(key string, out any) error

func (*RedisClient) GetConn

func (rc *RedisClient) GetConn() redis.Conn

func (*RedisClient) GetConnWithContext

func (rc *RedisClient) GetConnWithContext(ctx context.Context) (redis.Conn, error)

func (*RedisClient) GetPool added in v1.1.9

func (rc *RedisClient) GetPool() *redis.Pool

func (*RedisClient) HDel added in v1.1.16

func (rc *RedisClient) HDel(key string, fields ...string) error

HDel 删除hash字段

func (*RedisClient) HExists added in v1.1.16

func (rc *RedisClient) HExists(key, field string) (bool, error)

HExists 检查hash字段是否存在

func (*RedisClient) HGet added in v1.1.16

func (rc *RedisClient) HGet(key, field string, out any) error

HGet 获取hash字段的值

func (*RedisClient) HMGet added in v1.1.16

func (rc *RedisClient) HMGet(key string, fields []string, out map[string]any) error

HMGet 批量获取hash字段

func (*RedisClient) HMSet added in v1.1.16

func (rc *RedisClient) HMSet(key string, fields map[string]any) error

HMSet 批量设置hash字段

func (*RedisClient) HSet added in v1.1.16

func (rc *RedisClient) HSet(key, field string, value any) error

HSet 设置hash字段的值

func (*RedisClient) LLen added in v1.1.12

func (rc *RedisClient) LLen(key string) (int, error)

LLen returns the length of the list stored at key If the key does not exist, it is interpreted as an empty list and 0 is returned

Parameters: - key: The key of the list

Returns: - int: The length of the list at key - error: An error if the operation failed

func (*RedisClient) LPop added in v1.1.12

func (rc *RedisClient) LPop(key string, out any) error

LPop removes and returns the first element of the list stored at key If the list does not exist or is empty, it returns an error

Parameters: - key: The key of the list - out: A pointer to the variable where the popped value will be stored

Returns: - error: An error if the operation failed or if the list is empty

func (*RedisClient) LPush added in v1.1.12

func (rc *RedisClient) LPush(key string, values ...any) (int, error)

LPush pushes one or more values to the head of the list stored at key If the list does not exist, it is created as an empty list before performing the push operations

Parameters: - key: The key of the list - values: One or more values to push to the list

Returns: - int: The length of the list after the push operations - error: An error if the operation failed

func (*RedisClient) LRange added in v1.1.12

func (rc *RedisClient) LRange(key string, start, stop int, out any) error

LRange returns the specified elements of the list stored at key The offsets start and stop are zero-based indexes These offsets can also be negative numbers indicating offsets starting at the end of the list For example, -1 is the last element of the list, -2 the penultimate, and so on

Parameters: - key: The key of the list - start: The starting position (inclusive) - stop: The ending position (inclusive) - out: A pointer to the slice where the range of elements will be stored

Returns: - error: An error if the operation failed

func (*RedisClient) Lock

func (rc *RedisClient) Lock(key string, expires ...time.Duration) error

Lock attempts to acquire a lock with the given key in Redis. If the lock is successfully acquired, it sets an expiration time. The expiration time can be specified as a variadic argument; if not provided, a default expiration time (defaultTTL) will be used.

The lock is acquired using the Redis SET command with the "NX" option, which ensures that the lock is only set if the key does not already exist.

If the lock is already held (i.e., the key exists), the method returns ErrLockFailed. If any other error occurs during the operation, it is returned.

Parameters: - key: The key under which the lock is to be stored. - expires: Optional duration(s) for which the lock should be valid.

Returns: - An error, if the lock could not be acquired or another error occurred.

func (*RedisClient) LockWithBlock

func (rc *RedisClient) LockWithBlock(key string, maxRetry int, expires ...time.Duration) error

LockWithBlock attempts to acquire a lock with the given key in Redis, retrying up to a specified maximum number of attempts (maxRetry). If the lock cannot be acquired, it will wait for 500 milliseconds before retrying, as long as the error returned is ErrLockFailed.

The lock expiration time can be specified as a variadic argument; if not provided, a default expiration time will be used.

If the lock is successfully acquired, the method returns nil. If the maximum number of retries is reached without acquiring the lock, it returns ErrLockFailed.

Parameters: - key: The key under which the lock is to be stored. - maxRetry: The maximum number of retry attempts to acquire the lock. - expires: Optional duration(s) for which the lock should be valid.

Returns:

  • An error if the lock could not be acquired after maxRetry attempts, or if another error occurred during the lock acquisition process.

func (*RedisClient) MGet added in v1.1.16

func (rc *RedisClient) MGet(keys []string, out map[string]any) error

MGet 批量获取多个key的值

func (*RedisClient) MSet added in v1.1.16

func (rc *RedisClient) MSet(values map[string]any) error

MSet 批量设置多个key-value

func (*RedisClient) RPop added in v1.1.12

func (rc *RedisClient) RPop(key string, out any) error

RPop removes and returns the last element of the list stored at key If the list does not exist or is empty, it returns an error

Parameters: - key: The key of the list - out: A pointer to the variable where the popped value will be stored

Returns: - error: An error if the operation failed or if the list is empty

func (*RedisClient) RPush added in v1.1.12

func (rc *RedisClient) RPush(key string, values ...any) (int, error)

RPush appends one or more values to the tail of the list stored at key If the list does not exist, it is created as an empty list before performing the push operations

Parameters: - key: The key of the list - values: One or more values to push to the list

Returns: - int: The length of the list after the push operations - error: An error if the operation failed

func (*RedisClient) Rename added in v1.1.16

func (rc *RedisClient) Rename(oldKey, newKey string) error

Rename 重命名key

func (*RedisClient) SAdd added in v1.1.16

func (rc *RedisClient) SAdd(key string, members ...any) error

SAdd 向集合添加成员

func (*RedisClient) SCard added in v1.1.16

func (rc *RedisClient) SCard(key string) (int, error)

SCard 获取集合成员数量

func (*RedisClient) SIsMember added in v1.1.16

func (rc *RedisClient) SIsMember(key string, member any) (bool, error)

SIsMember 判断元素是否是集合成员

func (*RedisClient) SMembers added in v1.1.16

func (rc *RedisClient) SMembers(key string, out any) error

SMembers 获取集合所有成员

func (*RedisClient) SRem added in v1.1.16

func (rc *RedisClient) SRem(key string, members ...any) error

SRem 移除集合成员

func (*RedisClient) Set

func (rc *RedisClient) Set(key string, value any) error

func (*RedisClient) SetEX added in v1.1.16

func (rc *RedisClient) SetEX(key string, value any, seconds int) error

SetEX 设置带过期时间的键值对

func (*RedisClient) SetNX added in v1.1.16

func (rc *RedisClient) SetNX(key string, value any) (bool, error)

SetNX 仅当key不存在时设置值

func (*RedisClient) SetWithTTL

func (rc *RedisClient) SetWithTTL(key string, value any, ttl time.Duration) error

func (*RedisClient) TransactionPipeline

func (rc *RedisClient) TransactionPipeline(conn redis.Conn, watchKey []string, commands ...[]any) error

func (*RedisClient) UnLock

func (rc *RedisClient) UnLock(key string) error

type RedisConf

type RedisConf struct {
	Server   string `desc:"redis server name (default localhost:6379)"`
	Password string `desc:"redis server password"`
	Db       int    `desc:"redis db (default 0)"`
	MaxIdle  int    `desc:"redis maxIdle (default 100)"`
}

func (*RedisConf) DialRedisPool

func (conf *RedisConf) DialRedisPool() *redis.Pool

func (*RedisConf) SetDefault

func (conf *RedisConf) SetDefault()

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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