代码拉取完成,页面将自动刷新
package redisClient
import (
"encoding/json"
"fmt"
"gitee.com/lv_baobao/gcore"
"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()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。