redis

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 15 Imported by: 0

README

Redis

  1. 现状

    当前IDC集群中(开发/测试/线上)都是部署哨兵模式,但是公有云环境是主备模式,为了兼容2套环境,需要对redis api进行改造。

  2. 设计目标

  1. 示例

    redis主备模式集群配置

    redis.default.addr: "127.0.0.1:6379"
    redis.default.password: ""
    redis.default.db: 0
    redis.default.mode: "master" # 默认是哨兵模式,主备模式需要配置master
    

    redis使用

    import "github.com/SigmaGavin/slardar/redis"
    
    ctx := context.TODO()
    key := ""
    
    conn := redis.Get("default")
    value, err := conn.Get(ctx, key)
    if err != nil {
            return
    }
    

    redis哨兵模式集群配置

    redis.idc.addr: "10.19.22.20:26379,10.19.22.21:26379,10.19.22.22:26379"
    redis.idc.password: "oBzMhkl6w2Df1WFbssWq"
    redis.idc.db: 1
    redis.idc.mode: "" // 空串或者不配置该参数都是表示哨兵模式
    

    redis使用

    import "github.com/SigmaGavin/slardar/redis"
    
    ctx := context.TODO()
    key := ""
    value := ""
    
    conn := redis.Get("idc")
    err := conn.Set(ctx, key, value)
    if err != nil {
            return
    }
    
  2. QA

    1. Q: 为什么使用redigo库? A: 因为支持连接池

    2. Q: 为什么放在utils包下? A: 因为redis功能和业务无关,不应该放到业务包中,也方便其他app引用

    3. Q: test case如何跑起来? A: 首先设置环境变量"CONF_PATH",在goland中可以直接run

Documentation

Index

Constants

View Source
const (
	// ModelOfMaster 主从模式,默认是哨兵模式,需要配置/环境变量: olympus.redis.${name}.mode: "master"
	ModelOfMaster = "master"
)

Variables

This section is empty.

Functions

func EvalInt64

func EvalInt64(reply interface{}, err error) (int64, error)

EvalInt64 解析eval返回值

func GetSync

func GetSync(name string) (lock *redsync.Redsync)

GetSync 获取分布式锁 TODO: 这里维护了2个同样的连接池,需要合并一个

func IsCacheMiss

func IsCacheMiss(err error) bool

IsCacheMiss 判断是否存在该缓存

Types

type Client

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

Client redis client 封装

func Get

func Get(name string) (conn *Client)

Get 获取指定

func (*Client) Decr

func (c *Client) Decr(ctx context.Context, key string, value int64) (result int64, err error)

Decr 对指定的key减去一个数值

func (*Client) Del

func (c *Client) Del(ctx context.Context, key string) (err error)

Del 删除指定key

func (*Client) Eval

func (c *Client) Eval(ctx context.Context, script string, keys []string, argvs ...interface{}) (reply interface{}, err error)

Eval 运行脚本

func (*Client) Exists

func (c *Client) Exists(ctx context.Context, keys ...string) (result bool, err error)

Exists 指定的key是否存在

func (*Client) Expire

func (c *Client) Expire(ctx context.Context, key string, ttl int32) (err error)

Expire 设置过期时间,单位:秒

func (*Client) Get

func (c *Client) Get(ctx context.Context, key string) (value string, err error)

Get 查询单个缓存

func (*Client) HDel

func (c *Client) HDel(ctx context.Context, hashKey, key string) (err error)

HDel 删除 hash set 中指定的 key

func (*Client) HGet

func (c *Client) HGet(ctx context.Context, hashKey, key string) (value string, err error)

HGet 获取 hash set 中指定的 key

func (*Client) HGetAll

func (c *Client) HGetAll(ctx context.Context, hashKey string) (list map[string]string, err error)

HGetAll 获取 hash set 中所有的 kv

func (*Client) HIncrBy

func (c *Client) HIncrBy(ctx context.Context, hashKey, key string, value int64) (result int64, err error)

HIncrBy hash set 增加 key 指定的哈希集中指定字段的数值

func (*Client) HSet

func (c *Client) HSet(ctx context.Context, hashKey, key, value string) (err error)

HSet 设置 hash set 中的 kv

func (*Client) Incr

func (c *Client) Incr(ctx context.Context, key string, value int64) (result int64, err error)

Incr 对指定的key增加一个数值

func (*Client) LPOP

func (c *Client) LPOP(ctx context.Context, key string) (value string, err error)

LPOP 从最左边移除值 并返回

func (*Client) LPUSH

func (c *Client) LPUSH(ctx context.Context, key, value string) (err error)

LPUSH 从左边向列表中PUSH值

func (*Client) Llen

func (c *Client) Llen(ctx context.Context, key string) (value int, err error)

Llen 获取列表长度

func (*Client) RPOP

func (c *Client) RPOP(ctx context.Context, key string) (value string, err error)

RPOP 从最右边移除值 并返回

func (*Client) RPUSH

func (c *Client) RPUSH(ctx context.Context, key, value string) (err error)

RPUSH 从右边向列表中PUSH值

func (*Client) SAdd

func (c *Client) SAdd(ctx context.Context, key, value string) (err error)

SAdd 为集合增加一个元素

func (*Client) SIsMember

func (c *Client) SIsMember(ctx context.Context, key, value string) (isMember int, err error)

SIsMember 查询元素是否是集合的成员

func (*Client) SMembers

func (c *Client) SMembers(ctx context.Context, key string) (list []string, err error)

SMembers 获取集合中的所有的成员

func (*Client) SRem

func (c *Client) SRem(ctx context.Context, key, value string) (err error)

SRem 从集合中删除一个元素

func (*Client) Set

func (c *Client) Set(ctx context.Context, key, value string) (err error)

Set 设置单个缓存

func (*Client) SetNx

func (c *Client) SetNx(ctx context.Context, key string, value string, expire int32) (success bool, err error)

SetNx 如果key不存在,则将value写入, 同时设置key的超时时间(毫秒) 返回是否写入成功

func (*Client) SetPx

func (c *Client) SetPx(ctx context.Context, key string, value string, expire int32) (err error)

SetPx 设置缓存和时间,毫秒 SET ... PX 设置键的过期时间为 millisecond 毫秒 SET ... EX 设置键的过期时间为 second 秒

func (*Client) TTL

func (c *Client) TTL(ctx context.Context, key string) (ttl int, err error)

TTL 获取ttl

type Option

type Option struct {
	Addr     string
	Password string
	DB       int32
	Mode     string
}

Option 获取redis配置

Jump to

Keyboard shortcuts

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