You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
233 lines
5.5 KiB
233 lines
5.5 KiB
package redis
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/go-redis/redis"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
"wss-pool/config"
|
|
"wss-pool/logging/applogger"
|
|
)
|
|
|
|
var RedisClient *redis.Client
|
|
var RedisClientMap = map[string]*redis.Client{}
|
|
|
|
// RedisInit init Redis
|
|
func RedisInit(db int) *redis.Client {
|
|
addr := fmt.Sprintf("%v:%v", config.Config.Redis.Server, config.Config.Redis.Port)
|
|
client := redis.NewClient(&redis.Options{
|
|
Addr: addr,
|
|
DB: db, // use default DB
|
|
Password: config.Config.Redis.PassWord, // no password set
|
|
})
|
|
_, err := client.Ping().Result()
|
|
if err != nil {
|
|
applogger.Error("Failed to connect to Redis, terminating startup err: %v", err)
|
|
return nil
|
|
} else {
|
|
applogger.Info("redis init success")
|
|
}
|
|
|
|
return client
|
|
}
|
|
|
|
func RedisInitMap(dbs []string) {
|
|
RedisClientMap = make(map[string]*redis.Client)
|
|
for key, db := range dbs {
|
|
dbInt, _ := strconv.Atoi(db) // 表库
|
|
server := config.Config.Redis.Server // Ip
|
|
password := config.Config.Redis.PassWord // 密码
|
|
addr := fmt.Sprintf("%v:%v", server, config.Config.Redis.Port) // 0.0.0.0:6379
|
|
client := redis.NewClient(&redis.Options{
|
|
Addr: addr,
|
|
DB: dbInt,
|
|
Password: password,
|
|
})
|
|
_, err := client.Ping().Result()
|
|
if err != nil {
|
|
os.Exit(dbInt)
|
|
} else {
|
|
applogger.Info("redis init success")
|
|
}
|
|
if key == 0 {
|
|
RedisClient = client
|
|
}
|
|
RedisClientMap[server] = client
|
|
}
|
|
}
|
|
|
|
// TODO: 初始化多个Redis客户端
|
|
func RedisInitMapList(addrList map[string]string) {
|
|
RedisClientMap = make(map[string]*redis.Client)
|
|
for addr, password := range addrList {
|
|
client := redis.NewClient(&redis.Options{
|
|
Addr: addr,
|
|
DB: 0,
|
|
Password: password,
|
|
})
|
|
_, err := client.Ping().Result()
|
|
if err != nil {
|
|
os.Exit(0)
|
|
} else {
|
|
applogger.Info("redis init success")
|
|
}
|
|
RedisClientMap[addr] = client
|
|
}
|
|
}
|
|
|
|
// Get_Cache_Data Query data through key
|
|
func Get_Cache_Data(key string) (string, error) {
|
|
rge, err := RedisClient.Get(key).Result()
|
|
if err != nil {
|
|
applogger.Error("Redis Get err: %v", err)
|
|
return "", err
|
|
}
|
|
|
|
return rge, nil
|
|
}
|
|
|
|
// Get_Cache_Data Query data through key
|
|
|
|
func Get_Cache_Byte(key string) ([]byte, error) {
|
|
rge, err := RedisClient.Get(key).Bytes()
|
|
if err != nil {
|
|
applogger.Error("Redis Get err: %v", err)
|
|
return rge, err
|
|
}
|
|
|
|
return rge, nil
|
|
}
|
|
|
|
// Set_Cache_Data Set the key lifecycle
|
|
func Set_Cache_Data(key string, value interface{}, td int) error {
|
|
if err := RedisClient.Set(key, value, time.Duration(td)*time.Minute).Err(); err != nil {
|
|
applogger.Error("Redis Set err: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Get_Cache_Keys Query all keys
|
|
func Get_Cache_Keys() ([]string, error) {
|
|
rge, err := RedisClient.Keys("*").Result()
|
|
if err != nil {
|
|
applogger.Error("Redis Get err: %v", err)
|
|
return []string{}, err
|
|
}
|
|
|
|
return rge, nil
|
|
}
|
|
|
|
// Get_Cache_Count Query the total number of Redis
|
|
func Get_Cache_Count(key string) (int64, error) {
|
|
rge, err := RedisClient.DbSize().Result()
|
|
if err != nil {
|
|
applogger.Error("RedisClient DbSize err:%v", err)
|
|
return rge, err
|
|
}
|
|
return rge, nil
|
|
}
|
|
|
|
// Set_Cache_Value persistent data
|
|
func Set_Cache_Value(key, value string) error {
|
|
if err := RedisClient.Set(key, value, 0).Err(); err != nil {
|
|
applogger.Error("Redis Set err: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func HsetMap(key, field string, value interface{}) {
|
|
for k, db := range RedisClientMap {
|
|
applogger.Info("key", key, "field", field, "value", value, "DB", k)
|
|
err := db.HSet(key, field, value).Err()
|
|
if err != nil {
|
|
fmt.Println("db", k, "存储失败:", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func PublishMap(channel string, message interface{}) {
|
|
for k, db := range RedisClientMap {
|
|
applogger.Info("channel", channel, "DB", k)
|
|
err := db.Publish(channel, message).Err()
|
|
if err != nil {
|
|
fmt.Println("db", k, "存储失败:", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func ScanMap(prefix string) map[string][]string {
|
|
scanMap := make(map[string][]string)
|
|
for k, db := range RedisClientMap {
|
|
var keys []string
|
|
var err error
|
|
keys, err = db.Keys(prefix + "*").Result()
|
|
if err != nil {
|
|
applogger.Error(err.Error())
|
|
}
|
|
scanMap[k] = keys
|
|
}
|
|
return scanMap
|
|
}
|
|
|
|
func HGetAllMap(db string, key string) (map[string]string, error) {
|
|
return RedisClientMap[db].HGetAll(key).Result()
|
|
}
|
|
|
|
func Hset(key, field string, value interface{}) error {
|
|
applogger.Info("key", key, "field", field, "value", value)
|
|
err := RedisClient.HSet(key, field, value).Err()
|
|
if err != nil {
|
|
fmt.Println("存储失败:", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Expire(key string, expiration time.Duration) error {
|
|
err := RedisClient.Expire(key, expiration).Err()
|
|
if err != nil {
|
|
fmt.Println("设置过期时间:", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
func Hget(key, field string) (string, error) {
|
|
value, err := RedisClient.HGet(key, field).Result()
|
|
if err != nil {
|
|
return value, err
|
|
}
|
|
return value, nil
|
|
}
|
|
func HGetNew(key, field string, red *redis.Client) (string, error) {
|
|
value, err := red.HGet(key, field).Result()
|
|
if err != nil {
|
|
return value, err
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func HDel(key string, fields string) error {
|
|
if err := RedisClient.HDel(key, fields).Err(); err != nil {
|
|
fmt.Println("del error:", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func HGetAll(key string) (map[string]string, error) {
|
|
return RedisClient.HGetAll(key).Result()
|
|
}
|
|
|
|
func Scan(prefix string) []string {
|
|
var keys []string
|
|
var err error
|
|
keys, err = RedisClient.Keys(prefix + "*").Result()
|
|
if err != nil {
|
|
applogger.Error(err.Error())
|
|
}
|
|
//fmt.Println(keys)
|
|
return keys
|
|
}
|
|
|