Documentation
¶
Index ¶
- Variables
- type RedisClient
- func (rc *RedisClient) Close() error
- func (rc *RedisClient) Delete(key string) error
- func (rc *RedisClient) Do(command string, args ...any) (reply any, err error)
- func (rc *RedisClient) DoWithTransactionPipeline(watchKey []string, commands ...[]any) error
- func (rc *RedisClient) Get(key string, out any) error
- func (rc *RedisClient) GetConn() redis.Conn
- func (rc *RedisClient) GetConnWithContext(ctx context.Context) (redis.Conn, error)
- func (rc *RedisClient) Lock(key string, expires ...time.Duration) (err error)
- func (rc *RedisClient) LockWithBlock(key string, maxRetry int, expires ...time.Duration) (err error)
- func (rc *RedisClient) Set(key string, value any) error
- func (rc *RedisClient) SetWithTTL(key string, value any, ttl time.Duration) error
- func (rc *RedisClient) TransactionPipeline(conn redis.Conn, watchKey []string, commands ...[]any) error
- func (rc *RedisClient) UnLock(key string) (err error)
- type RedisConf
Constants ¶
This section is empty.
Variables ¶
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) GetConn ¶
func (rc *RedisClient) GetConn() redis.Conn
func (*RedisClient) GetConnWithContext ¶
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) SetWithTTL ¶
func (*RedisClient) TransactionPipeline ¶
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 (*RedisConf) SetDefault ¶
func (conf *RedisConf) SetDefault()