rredis

package module
v0.0.0-...-9b1b795 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: MIT Imports: 4 Imported by: 0

README

redis基础封装*(基于go-redis)


ctrl c/v永不过时 !!!

├─delayedQueue.go          实现了一个基于redis的延时队列(不支持重试和死信)
├─lock.go                  简单实现了一个基于redis的分布式锁方案
├─redis.go                 基于redis配置实现一个可以支持集群或者单点的初始化类
├─script.go                存放了一些在工作用使用到的脚本比如扣减库存
├─subscribe.go             实现了一个简单的redis订阅发布的封装
├─struct_to_map            golang的struct序列化到redis的hash里面(偷懒用,不建议使用,底层使用反射,这玩意懂得都懂)
│    ├─decode.go               解码
│    ├─encode.go               加码
│    ├─map.go                  底层使用反射解析代码(原理其实很简单,基础数据不用管,不是基础数据的可以用proto序列化就用proto,不能的话,直接干json,所以慢而且占内存,但胜在方便)
│    ├─struct_to_map.go        封装出对外使用接口
│    ├─tag.go                  tag解析

Documentation

Index

Constants

This section is empty.

Variables

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 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

This section is empty.

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"`
}

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, channelName, msg string)

func (*RedisSubscribe) SubMessage

func (r *RedisSubscribe) SubMessage(ctx context.Context, channelName string, msgChan chan string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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