101 lines
2.7 KiB
Go
101 lines
2.7 KiB
Go
package umredis
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"AngkorWalletScanning/umlog"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
// version : 1.0.0
|
|
// date : 2022-10-19
|
|
// writer : lee.beomhee
|
|
// modify : lee.beomhee (2022-10-19)
|
|
|
|
func RedisClusterConnect(host []string, password string) *redis.ClusterClient {
|
|
var rdb *redis.ClusterClient
|
|
if len(password) > 0 {
|
|
rdb = redis.NewClusterClient(&redis.ClusterOptions{
|
|
Addrs: host,
|
|
Password: password,
|
|
})
|
|
} else {
|
|
rdb = redis.NewClusterClient(&redis.ClusterOptions{
|
|
Addrs: host,
|
|
})
|
|
}
|
|
return rdb
|
|
}
|
|
|
|
func RedisClusterDisConnect(cluster *redis.ClusterClient) {
|
|
umlog.Debug("REDIS DISCONNECTION")
|
|
cluster.Close()
|
|
}
|
|
|
|
func ClusterGetKeys(cluster *redis.ClusterClient, keys string) ([]string, error) {
|
|
return cluster.Keys(context.Background(), keys+"*").Result()
|
|
}
|
|
|
|
func ClusterGetValue(cluster *redis.ClusterClient, key string) (string, error) {
|
|
|
|
return cluster.Get(context.Background(), key).Result()
|
|
}
|
|
|
|
func ClusterHGetValue(cluster *redis.ClusterClient, key string, field string) (string, error) {
|
|
return cluster.HGet(context.Background(), key, field).Result()
|
|
}
|
|
|
|
func ClusterHGetAllValue(cluster *redis.ClusterClient, key string) (map[string]string, error) {
|
|
return cluster.HGetAll(context.Background(), key).Result()
|
|
}
|
|
|
|
func ClusterSetValue(cluster *redis.ClusterClient, key string, value any) error {
|
|
_, err := cluster.Set(context.Background(), key, value, 0).Result()
|
|
return err
|
|
}
|
|
|
|
func ClusterHMSetValue(cluster *redis.ClusterClient, key string, value any) error {
|
|
_, err := cluster.HMSet(context.Background(), key, value).Result()
|
|
return err
|
|
}
|
|
|
|
func ClusterHSetValue(cluster *redis.ClusterClient, key string, field string, value any) error {
|
|
_, err := cluster.HSet(context.Background(), key, field, value).Result()
|
|
return err
|
|
}
|
|
|
|
func ClusterHINCRBY(cluster *redis.ClusterClient, key string, field string, value int64) error {
|
|
_, err := cluster.HIncrBy(context.Background(), key, field, value).Result()
|
|
return err
|
|
}
|
|
|
|
func ClusterRemoveValue(cluster *redis.ClusterClient, key string, field string) error {
|
|
_, err := cluster.HDel(context.Background(), key, field).Result()
|
|
return err
|
|
}
|
|
|
|
func ClusterRemoveKey(cluster *redis.ClusterClient, key string) error {
|
|
_, err := cluster.Del(context.Background(), key).Result()
|
|
return err
|
|
}
|
|
|
|
func ClusterRemoveKeys(cluster *redis.ClusterClient, keys string) error {
|
|
findKeys, err := cluster.Keys(context.Background(), keys+"*").Result()
|
|
if err == nil {
|
|
for _, key := range findKeys {
|
|
_, err = cluster.Del(context.Background(), key).Result()
|
|
if err != nil {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func ClusterExpireKey(cluster *redis.ClusterClient, key string, sec time.Duration) error {
|
|
_, err := cluster.Expire(context.Background(), key, sec).Result()
|
|
return err
|
|
}
|