predis

package
v1.1.15 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: MIT Imports: 8 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")
)

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) 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) (err 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) (err 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) 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) Set

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

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) (err 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