Documentation
¶
Index ¶
- Constants
- Variables
- type DistributedLock
- type DistributedLockOption
- func WithDebug() DistributedLockOption
- func WithExpiration(t time.Duration) DistributedLockOption
- func WithIdfa(name string) DistributedLockOption
- func WithIoWriter(w io.Writer) DistributedLockOption
- func WithKey(key string) DistributedLockOption
- func WithNodeLockInterval(t time.Duration) DistributedLockOption
- func WithNodeLockTimeout(t time.Duration) DistributedLockOption
- func WithRedisClient(cli *redis.Client) DistributedLockOption
- func WithRedisLockInterval(t time.Duration) DistributedLockOption
- func WithRedisLockTimeout(t time.Duration) DistributedLockOption
- func WithWatchdogRate(t time.Duration) DistributedLockOption
- type Redis
- func (r *Redis) CacheWithString(key string, output interface{}, expire time.Duration, ...) error
- func (r *Redis) DeductStock(key string, quantity int) (bool, error)
- func (r *Redis) FrequencyLimit(key interface{}, second uint8) bool
- func (r *Redis) HGetAll(key string) (result []interface{})
- func (r *Redis) HMDel(key string, fields ...string) error
- func (r *Redis) HMSet(key string, data map[string]interface{}) error
- func (r *Redis) NewRedis(collector *types.RedisCollector, client *types.RedisClient, ...) (*redis.Client, error)
- func (r *Redis) NoDataExpiration() time.Duration
Constants ¶
View Source
const FrequencyLimitKey = "rds@freq:limit:%v"
View Source
const HashAllFieldKey = "all"
Variables ¶
View Source
var ( ErrRedisLockKeyEmpty = errors.New("redis lock key cannot be an empty string") ErrRedisClientNil = errors.New("redis client cannot be nil") ErrGetNodeLockTimeout = errors.New("timeout get node lock") ErrGetRedisLockTimeout = errors.New("timeout get redis lock") )
View Source
var ( ErrOutOfStock = errors.New("out of stock") ErrInvalidField = errors.New("invalid field") )
Functions ¶
This section is empty.
Types ¶
type DistributedLock ¶ added in v1.3.2
type DistributedLock struct { Key string // redis锁key Expiration time.Duration // redis锁key有效时间 NodeLockTimeout time.Duration // 获取节点锁超时时间 RedisLockTimeout time.Duration // 获取redis锁超时时间 NodeLockInterval time.Duration // 循环获取节点锁间隔时间 RedisLockInterval time.Duration // 循环获取redis锁间隔时间 WatchdogRate time.Duration // 看门狗检测频率,注意:此时间必须小于RedisLockTimeout,否则看门狗不会执行 // contains filtered or unexported fields }
DistributedLock redis分布式锁
func NewDistributedLock ¶ added in v1.3.2
func NewDistributedLock(opts ...DistributedLockOption) (*DistributedLock, error)
func (*DistributedLock) Exec ¶ added in v1.3.2
func (dl *DistributedLock) Exec(fn func(*DistributedLock) (interface{}, error)) (interface{}, error)
func (*DistributedLock) RedisClient ¶ added in v1.3.2
func (dl *DistributedLock) RedisClient() *redis.Client
func (*DistributedLock) RedisLockKey ¶ added in v1.3.2
func (dl *DistributedLock) RedisLockKey() string
type DistributedLockOption ¶ added in v1.3.2
type DistributedLockOption func(dl *DistributedLock)
func WithDebug ¶ added in v1.3.2
func WithDebug() DistributedLockOption
func WithExpiration ¶ added in v1.3.2
func WithExpiration(t time.Duration) DistributedLockOption
func WithIdfa ¶ added in v1.3.2
func WithIdfa(name string) DistributedLockOption
func WithIoWriter ¶ added in v1.3.2
func WithIoWriter(w io.Writer) DistributedLockOption
func WithKey ¶ added in v1.3.2
func WithKey(key string) DistributedLockOption
func WithNodeLockInterval ¶ added in v1.3.2
func WithNodeLockInterval(t time.Duration) DistributedLockOption
func WithNodeLockTimeout ¶ added in v1.3.2
func WithNodeLockTimeout(t time.Duration) DistributedLockOption
func WithRedisClient ¶ added in v1.3.2
func WithRedisClient(cli *redis.Client) DistributedLockOption
func WithRedisLockInterval ¶ added in v1.3.2
func WithRedisLockInterval(t time.Duration) DistributedLockOption
func WithRedisLockTimeout ¶ added in v1.3.2
func WithRedisLockTimeout(t time.Duration) DistributedLockOption
func WithWatchdogRate ¶ added in v1.3.2
func WithWatchdogRate(t time.Duration) DistributedLockOption
type Redis ¶
func (*Redis) CacheWithString ¶
func (r *Redis) CacheWithString(key string, output interface{}, expire time.Duration, getDataFunc func() (data interface{}, isNull bool)) error
CacheWithString 从String类型的缓存中读取数据,如没则重新调用指定方法重新从数据库中读取并写入缓存
func (*Redis) DeductStock ¶ added in v1.3.40
DeductStock 减库存,通过lua脚本执行以防超卖
func (*Redis) FrequencyLimit ¶ added in v1.3.40
FrequencyLimit 频率限制, 返回ture代表频率过高
func (*Redis) NewRedis ¶
func (r *Redis) NewRedis(collector *types.RedisCollector, client *types.RedisClient, optsFunc func() *redis.Options) (*redis.Client, error)
func (*Redis) NoDataExpiration ¶
NoDataExpiration 数据不存在情况下,为防止缓存雪崩,随机返回一个30到60秒的有效时间
Click to show internal directories.
Click to hide internal directories.