1 Star 0 Fork 0

xingang / gcore2

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ClusterClient.go 3.18 KB
一键复制 编辑 原始数据 按行查看 历史
xingang 提交于 2023-02-15 14:52 . gcore2
package redisClient
import (
"encoding/json"
"fmt"
"gitee.com/qq358678184_admin/gcore2"
"github.com/ahmetb/go-linq/v3"
"github.com/go-redis/redis"
"runtime"
"time"
)
type ClusterClient struct {
Client *redis.ClusterClient
}
func NewClusterClient() IRedisClient {
var clusterRedisClient = &ClusterClient{}
clusterRedisClient.init()
return clusterRedisClient
}
func (r *ClusterClient) init() {
redisConfig := gcore.NewAppSettingsHelper().GetAppConfig().Redis
serverList := redisConfig.ServerList
if redisConfig.PoolSize == 0 {
redisConfig.PoolSize = runtime.NumCPU()
}
fmt.Printf("redis poolsize %d", redisConfig.PoolSize)
var addrList = make([]string, 0)
linq.From(serverList).Select(func(i interface{}) interface{} {
server := i.(gcore.RedisServer)
return fmt.Sprintf("%s:%d", server.Host, server.Port)
}).ToSlice(&addrList)
r.Client = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: addrList,
Password: redisConfig.PassWord,
PoolSize: redisConfig.PoolSize,
})
}
func (r *ClusterClient) Get(key string, objPtr interface{}) error {
bytes, err := r.Client.Get(key).Bytes()
if err != nil {
return err
}
err = json.Unmarshal(bytes, objPtr)
if err != nil {
return err
}
return nil
}
func (r *ClusterClient) Incr(key string) error {
return r.Client.Incr(key).Err()
}
func (r *ClusterClient) Add(key string, value interface{}, duration time.Duration) error {
bytes, err := json.Marshal(value)
if err != nil {
return err
}
return r.Client.Set(key, bytes, duration).Err()
}
func (r *ClusterClient) Del(key string) error {
return r.Client.Del(key).Err()
}
func (r *ClusterClient) Expire(key string, duration time.Duration) error {
return r.Client.Expire(key, duration).Err()
}
func (r *ClusterClient) LPush(key string, value interface{}) (error, int64) {
bytes, err := json.Marshal(value)
if err != nil {
return err, 0
}
push := r.Client.LPush(key, bytes)
return push.Err(), push.Val()
}
func (r *ClusterClient) LRange(key string, start int64, stop int64) ([]string, error) {
push := r.Client.LRange(key, start, stop)
return push.Val(), push.Err()
}
func (r *ClusterClient) LIndex(key string, index int64) (string, error) {
push := r.Client.LIndex(key, index)
return push.Val(), push.Err()
}
func (r *ClusterClient) LSet(key string, index int64, value interface{}) error {
cmd := r.Client.LSet(key, index, value)
return cmd.Err()
}
func (r *ClusterClient) LRem(key string, count int64, value interface{}) error {
cmd := r.Client.LRem(key, count, value)
return cmd.Err()
}
func (r *ClusterClient) LTrim(key string, start int64, stop int64) error {
cmd := r.Client.LTrim(key, start, stop)
return cmd.Err()
}
func (r *ClusterClient) HSet(key string, field string, value interface{}) error {
bytes, err := json.Marshal(value)
if err != nil {
return err
}
cmd := r.Client.HSet(key, field, bytes)
return cmd.Err()
}
func (r *ClusterClient) HGet(key string, field string, objPtr interface{}) error {
cmd := r.Client.HGet(key, field)
if cmd.Err() != nil {
return cmd.Err()
}
bytes, err := cmd.Bytes()
if err != nil {
return err
}
err = json.Unmarshal(bytes, objPtr)
return err
}
func (r *ClusterClient) Pipeline() redis.Pipeliner {
return r.Client.Pipeline()
}
1
https://gitee.com/qq358678184_admin/gcore2.git
git@gitee.com:qq358678184_admin/gcore2.git
qq358678184_admin
gcore2
gcore2
a1f72da8efa7

搜索帮助

53164aa7 5694891 3bd8fe86 5694891