rredis

package
v0.0.0-...-1246224 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTypeNil = errors.New("数据为nil")
View Source
var ErrUnknownType = errors.New("未知数据类型")
View Source
var HIncrMinZeroScript = redis.NewScript(`if (redis.call('exists', KEYS[1]) == 1) then
				local stock = tonumber(redis.call('hget', KEYS[1], KEYS[2]));
				local num = tonumber(ARGV[1]);
				if (stock == -1) then
					return -1;
				end;
				if (stock + num >= 0) then
					return redis.call('HINCRBY', KEYS[1], KEYS[2], num);
				elseif (stock + num < 0) then
					return redis.call('HSET', KEYS[1], KEYS[2], 0)
				end;
				return -2;
			end;
			return -3;`)
View Source
var HIncrTopLimitScript = redis.NewScript(`
				local key = KEYS[1]
				local field = ARGV[1]
				local increment = tonumber(ARGV[2])
				local maxValue = tonumber(ARGV[3])
				local currentValue = tonumber(redis.call('HGET', key, field))
				if currentValue == nil then
					currentValue = 0
				end
				if currentValue == maxValue then
					return maxValue
				end
				local newValue = currentValue + increment
				if newValue > maxValue then
					newValue = maxValue
				end
				redis.call('HSET', key, field, newValue)
				return newValue
			`)
View Source
var HIncrUnMinusScript = redis.NewScript(`if (redis.call('exists', KEYS[1]) == 1) then
				local stock = tonumber(redis.call('hget', KEYS[1], KEYS[2]));
				local num = tonumber(ARGV[1]);
				if (stock == -1) then
					return -1;
				end;
				if (stock + num >= 0) then
					return redis.call('HINCRBY', KEYS[1], KEYS[2], num);
				end;
				return -2;
			end;
			return -3;`)
View Source
var IncrScript = redis.NewScript(`if (redis.call('exists', KEYS[1]) == 1) then
				local stock = tonumber(redis.call('get', KEYS[1]));
				local num = tonumber(ARGV[1]);
				if (stock == -1) then
					return -1;
				end;
				if (stock + num >= 0) then
					return redis.call('incrby', KEYS[1], num);
				end;
				return -2;
			end;
			return -3;`)
View Source
var IncrTopLimitScript = redis.NewScript(`
				local stock = tonumber(redis.call('get', KEYS[1]));
				if type(stock) == "nil" then
					stock = 0
				end;
				local num = tonumber(ARGV[1]);
				local top = tonumber(ARGV[2]);
				if ( stock + num > top) then
					return 0;
				else
					return redis.call('incrby', KEYS[1], num);
				end;
			`)

Functions

func StructToRedisHash

func StructToRedisHash(ctx context.Context, cli redis.Cmdable, key string, data interface{}) error

Types

type Client

type Client struct {
	redis.Cmdable
}

func NewRedis

func NewRedis(o Config, ctx context.Context) (client *Client, err error)

func (*Client) Close

func (c *Client) Close() error

func (*Client) Process

func (c *Client) Process(cmd redis.Cmder) error

type Config

type Config struct {
	PoolSize     int      `yaml:"poolSize"`
	Addr         []string `yaml:"addr"`
	Pwd          string   `yaml:"pwd"`
	DialTimeout  int64    `yaml:"dialTimeout"`
	ReadTimeout  int64    `yaml:"readTimeout"`
	WriteTimeout int64    `yaml:"writeTimeout"`
	DB           int      `yaml:"db"`
}

type DelayedQueue

type DelayedQueue interface {
	// 添加队列消息
	Put(ctx context.Context, z []*redis.Z) error
	// 处理单条队列消息
	DealWithOnce(ctx context.Context, min, max float64) (*redis.Z, error)
	// 处理多条队列消息
	DealWithMultiple(ctx context.Context, min, max float64, count int64) ([]redis.Z, error)
	// 队列消息加速
	Expedite(ctx context.Context, member string, score float64) error
}

func NewDelayedQueue

func NewDelayedQueue(rd *Client, fun QueueDealWith, keyName, lockKey, lockVal string, lockTtl time.Duration) DelayedQueue

type QueueDealWith

type QueueDealWith interface {
	DealWithOnce(z redis.Z) error
	DealWithMultiple(z []redis.Z) error
}

type RedisLock

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

func NewRedisLock

func NewRedisLock(conn redis.Cmdable, key, val string, timeout time.Duration) *RedisLock

func (*RedisLock) GetLockKey

func (lock *RedisLock) GetLockKey() string

func (*RedisLock) GetLockVal

func (lock *RedisLock) GetLockVal() string

func (*RedisLock) TryLock

func (lock *RedisLock) TryLock(ctx context.Context) (bool, error)

TryLock return true ===> Get the lock successfully

func (*RedisLock) UnLock

func (lock *RedisLock) UnLock(ctx context.Context) error

type RedisSubscribe

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

func NewRedisSubscribe

func NewRedisSubscribe(conn *redis.Client) *RedisSubscribe

func (*RedisSubscribe) PubMessage

func (r *RedisSubscribe) PubMessage(ctx context.Context, channel, msg string)

func (*RedisSubscribe) SubMessage

func (r *RedisSubscribe) SubMessage(ctx context.Context, channel, msg string)

Jump to

Keyboard shortcuts

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