123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- package redis
- import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "strings"
- "time"
- "github.com/go-redis/redis/v8"
- )
- // ClusterRedisClient
- // @Description: 集群的redis客户端
- type ClusterRedisClient struct {
- redisClient *redis.ClusterClient
- }
- var DefaultKey = "zcmRedis"
- // InitClusterRedis
- // @Description: 初始化集群redis客户端
- // @param config
- // @return clusterRedisClient
- // @return err
- func InitClusterRedis(config string) (clusterRedisClient *ClusterRedisClient, err error) {
- var cf map[string]string
- err = json.Unmarshal([]byte(config), &cf)
- if err != nil {
- return
- }
- // 集群地址
- connList := make([]string, 0)
- if _, ok := cf["conn"]; !ok {
- err = errors.New("config has no conn key")
- return
- } else {
- connList = strings.Split(cf["conn"], ",")
- if len(connList) <= 1 {
- err = errors.New("conn address less than or equal to 1")
- return
- }
- }
- // 密码
- if _, ok := cf["password"]; !ok {
- cf["password"] = ""
- }
- // 创建 Redis 客户端配置对象
- clusterOptions := &redis.ClusterOptions{
- Addrs: connList, // 设置 Redis 节点的 IP 地址和端口号
- Password: cf["password"],
- }
- // 创建 Redis 集群客户端
- client := redis.NewClusterClient(clusterOptions)
- // 测试连接并获取信息
- _, err = client.Ping(context.TODO()).Result()
- if err != nil {
- err = errors.New("redis 链接失败:" + err.Error())
- return
- }
- clusterRedisClient = &ClusterRedisClient{redisClient: client}
- return
- }
- // Get
- // @Description: 根据key获取数据(其实是返回的字节编码)
- // @receiver rc
- // @param key
- // @return interface{}
- func (rc *ClusterRedisClient) Get(key string) interface{} {
- data, err := rc.redisClient.Get(context.TODO(), key).Bytes()
- if err != nil {
- return nil
- }
- return data
- }
- // RedisBytes
- // @Description: 根据key获取字节编码数据
- // @receiver rc
- // @param key
- // @return data
- // @return err
- func (rc *ClusterRedisClient) RedisBytes(key string) (data []byte, err error) {
- data, err = rc.redisClient.Get(context.TODO(), key).Bytes()
- return
- }
- // RedisString
- // @Description: 根据key获取字符串数据
- // @receiver rc
- // @param key
- // @return data
- // @return err
- func (rc *ClusterRedisClient) RedisString(key string) (data string, err error) {
- data, err = rc.redisClient.Get(context.TODO(), key).Result()
- return
- }
- // RedisInt
- // @Description: 根据key获取int数据
- // @receiver rc
- // @param key
- // @return data
- // @return err
- func (rc *ClusterRedisClient) RedisInt(key string) (data int, err error) {
- data, err = rc.redisClient.Get(context.TODO(), key).Int()
- return
- }
- // Put
- // @Description: put一个数据到redis
- // @receiver rc
- // @param key
- // @param val
- // @param timeout
- // @return error
- func (rc *ClusterRedisClient) Put(key string, val interface{}, timeout time.Duration) error {
- var err error
- err = rc.redisClient.SetEX(context.TODO(), key, val, timeout).Err()
- if err != nil {
- return err
- }
- err = rc.redisClient.HSet(context.TODO(), DefaultKey, key, true).Err()
- return err
- }
- // SetNX
- // @Description: 设置一个会过期时间的值
- // @receiver rc
- // @param key
- // @param val
- // @param timeout
- // @return bool
- func (rc *ClusterRedisClient) SetNX(key string, val interface{}, timeout time.Duration) bool {
- result, err := rc.redisClient.SetNX(context.TODO(), key, val, timeout).Result()
- if err != nil {
- return false
- }
- return result
- }
- func (rc *ClusterRedisClient) SetEX(key string, val interface{}, timeout time.Duration) bool {
- _, err := rc.redisClient.SetEX(context.TODO(), key, val, timeout).Result()
- if err != nil {
- return false
- }
- return true
- }
- // Delete
- // @Description: 删除redis中的键值对
- // @receiver rc
- // @param key
- // @return error
- func (rc *ClusterRedisClient) Delete(key string) error {
- var err error
- err = rc.redisClient.Del(context.TODO(), key).Err()
- if err != nil {
- return err
- }
- err = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
- return err
- }
- // IsExist
- // @Description: 根据key判断是否写入缓存中
- // @receiver rc
- // @param key
- // @return bool
- func (rc *ClusterRedisClient) IsExist(key string) bool {
- result, err := rc.redisClient.Exists(context.TODO(), key).Result()
- if err != nil {
- return false
- }
- if result == 0 {
- _ = rc.redisClient.HDel(context.TODO(), DefaultKey, key).Err()
- return false
- }
- return true
- }
- // LPush
- // @Description: 写入list
- // @receiver rc
- // @param key
- // @param val
- // @return error
- func (rc *ClusterRedisClient) LPush(key string, val interface{}) error {
- data, _ := json.Marshal(val)
- err := rc.redisClient.LPush(context.TODO(), key, data).Err()
- return err
- }
- // Brpop
- // @Description: 从list中读取
- // @receiver rc
- // @param key
- // @param callback
- func (rc *ClusterRedisClient) Brpop(key string, callback func([]byte)) {
- values, err := rc.redisClient.BRPop(context.TODO(), 1*time.Second, key).Result()
- if err != nil {
- return
- }
- if len(values) < 2 {
- fmt.Println("assert is wrong")
- return
- }
- callback([]byte(values[1]))
- }
- // GetRedisTTL
- // @Description: 获取key的过期时间
- // @receiver rc
- // @param key
- // @return time.Duration
- func (rc *ClusterRedisClient) GetRedisTTL(key string) time.Duration {
- value, err := rc.redisClient.TTL(context.TODO(), key).Result()
- if err != nil {
- return 0
- }
- return value
- }
- // Incrby
- // @Description: 设置自增值
- // @receiver rc
- // @param key
- // @param num
- // @return interface{}
- // @return error
- func (rc *ClusterRedisClient) Incrby(key string, num int) (interface{}, error) {
- return rc.redisClient.IncrBy(context.TODO(), key, int64(num)).Result()
- }
- // Do
- // @Description: cmd执行redis命令
- // @receiver rc
- // @param commandName
- // @param args
- // @return reply
- // @return err
- func (rc *ClusterRedisClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
- newArgs := []interface{}{commandName}
- newArgs = append(newArgs, args...)
- return rc.redisClient.Do(context.TODO(), newArgs...).Result()
- }
- // RunLUA
- // @Description: cmd执行redis命令
- // @receiver rc
- // @param commandName
- // @param args
- // @return reply
- // @return err
- func (rc *ClusterRedisClient) RunLUA(command string, ctx context.Context, keys []string, args ...interface{}) bool {
- script := redis.NewScript(command)
- result, err := script.Run(ctx, rc.redisClient, keys, args...).Int()
- if err != nil {
- return false
- }
- if result == 1 {
- return true
- }
- return false
- }
|