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.
322 lines
7.1 KiB
322 lines
7.1 KiB
6 months ago
|
package utils
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"time"
|
||
|
|
||
|
"shop-api/global"
|
||
|
|
||
|
"github.com/go-redis/redis"
|
||
|
)
|
||
|
|
||
|
// RedisGet ...
|
||
|
func RedisGet(key string) string {
|
||
|
result, err := global.MG_REDIS.Get(key).Result()
|
||
|
if err != nil {
|
||
|
return ""
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
func RedisSet(key string, value interface{}, expiration time.Duration) string {
|
||
|
result, err := global.MG_REDIS.Set(key, value, expiration).Result()
|
||
|
if err != nil {
|
||
|
return ""
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
func RedisDel(key ...string) (int64, error) {
|
||
|
result, err := global.MG_REDIS.Del(key...).Result()
|
||
|
if err != nil {
|
||
|
return -1, err
|
||
|
}
|
||
|
return result, err
|
||
|
}
|
||
|
|
||
|
func RedisLLen(key string) (int64, error) {
|
||
|
result, err := global.MG_REDIS.LLen(key).Result()
|
||
|
if err != nil {
|
||
|
return -1, err
|
||
|
}
|
||
|
return result, err
|
||
|
}
|
||
|
|
||
|
// RedisHashSet 向key的hash中添加元素field的值
|
||
|
func RedisHashSet(key, field string, data interface{}) error {
|
||
|
err := global.MG_REDIS.HSet(key, field, data).Err()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// RedisBatchHashSet 批量向key的hash添加对应元素field的值
|
||
|
func RedisBatchHashSet(key string, fields map[string]interface{}) (error, string) {
|
||
|
val, err := global.MG_REDIS.HMSet(key, fields).Result()
|
||
|
if err != nil {
|
||
|
return err, ""
|
||
|
}
|
||
|
return nil, val
|
||
|
}
|
||
|
|
||
|
func RedisHDel(client *redis.Client, key string, fields ...string) (int64, string) {
|
||
|
result, err := client.HDel(key, fields...).Result()
|
||
|
if err != nil {
|
||
|
return 0, ""
|
||
|
}
|
||
|
return result, err.Error()
|
||
|
}
|
||
|
|
||
|
func RedisHSet(key, field string, value interface{}) (error, bool) {
|
||
|
val, err := global.MG_REDIS.HSet(key, field, value).Result()
|
||
|
if err != nil {
|
||
|
return err, false
|
||
|
}
|
||
|
return nil, val
|
||
|
}
|
||
|
|
||
|
func RedisSetNX(key string, value interface{}, expiration time.Duration) (error, bool) {
|
||
|
val, err := global.MG_REDIS.SetNX(key, value, expiration).Result()
|
||
|
if err != nil {
|
||
|
return err, false
|
||
|
}
|
||
|
return nil, val
|
||
|
}
|
||
|
|
||
|
// RedisHashGet 通过key获取hash的元素值
|
||
|
func RedisHashGet(key, field string) string {
|
||
|
val, err := global.MG_REDIS.HGet(key, field).Result()
|
||
|
if err != nil {
|
||
|
return ""
|
||
|
}
|
||
|
return val
|
||
|
}
|
||
|
|
||
|
func RedisHashMGet(key string, fields ...string) []interface{} {
|
||
|
val, err := global.MG_REDIS.HMGet(key, fields...).Result()
|
||
|
if err != nil {
|
||
|
return nil
|
||
|
}
|
||
|
return val
|
||
|
}
|
||
|
|
||
|
// HGetAll
|
||
|
func RedisHashGetAll(key string) map[string]string {
|
||
|
val, err := global.MG_REDIS.HGetAll(key).Result()
|
||
|
if err != nil {
|
||
|
return nil
|
||
|
}
|
||
|
return val
|
||
|
}
|
||
|
|
||
|
// RedisBatchHashGet 批量获取key的hash中对应多元素值
|
||
|
func RedisBatchHashGet(key string, fields ...string) map[string]interface{} {
|
||
|
resMap := make(map[string]interface{})
|
||
|
for _, field := range fields {
|
||
|
val, err := global.MG_REDIS.HGet(key, fmt.Sprintf("%s", field)).Result()
|
||
|
if err == nil && val != "" {
|
||
|
resMap[field] = val
|
||
|
}
|
||
|
}
|
||
|
return resMap
|
||
|
}
|
||
|
|
||
|
func RedisLPush(key string, values ...interface{}) (int64, error) {
|
||
|
result, err := global.MG_REDIS.LPush(key, values...).Result()
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func RedisLPop(key string) (error, string) {
|
||
|
result, err := global.MG_REDIS.LPop(key).Result()
|
||
|
if err != nil {
|
||
|
return err, ""
|
||
|
}
|
||
|
return err, result
|
||
|
}
|
||
|
|
||
|
// RedisSAdd 无序集合添加
|
||
|
func RedisSAdd(key string, values ...interface{}) (int64, error) {
|
||
|
result, err := global.MG_REDIS.SAdd(key, values...).Result()
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
// RedisSMembers 从无序集合中获取数据
|
||
|
func RedisSMembers(key string) ([]string, error) {
|
||
|
val, err := global.MG_REDIS.SMembers(key).Result()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return val, nil
|
||
|
}
|
||
|
|
||
|
// RedisZAdd 有序集合添加
|
||
|
func RedisZAdd(key string, values map[float64]interface{}) (int64, error) {
|
||
|
var members []redis.Z
|
||
|
for scale, value := range values {
|
||
|
members = append(members, redis.Z{
|
||
|
Score: scale,
|
||
|
Member: value,
|
||
|
})
|
||
|
}
|
||
|
result, err := global.MG_REDIS.ZAdd(key, members...).Result()
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func RedisZCard(key string) (int64, error) {
|
||
|
result, err := global.MG_REDIS.ZCard(key).Result()
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func RedisZCount(key string, min, max string) (int64, error) {
|
||
|
result, err := global.MG_REDIS.ZCount(key, min, max).Result()
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func RedisZRange(key string, start, stop int64, sort string) ([]interface{}, error) {
|
||
|
val := make([]interface{}, 0)
|
||
|
if sort == "asc" { // scale 正序
|
||
|
result, err := global.MG_REDIS.ZRangeWithScores(key, start, stop).Result()
|
||
|
if err != nil {
|
||
|
return val, err
|
||
|
}
|
||
|
for _, z := range result {
|
||
|
val = append(val, z.Member)
|
||
|
}
|
||
|
} else { // scale 倒序
|
||
|
result, err := global.MG_REDIS.ZRevRange(key, start, stop).Result()
|
||
|
if err != nil {
|
||
|
return val, err
|
||
|
}
|
||
|
for _, z := range result {
|
||
|
val = append(val, z)
|
||
|
}
|
||
|
return val, nil
|
||
|
}
|
||
|
|
||
|
return val, nil
|
||
|
}
|
||
|
|
||
|
// RedisIncr 获取自增唯一ID
|
||
|
func RedisIncr(key string) int {
|
||
|
val, err := global.MG_REDIS.Incr(key).Result()
|
||
|
if err != nil {
|
||
|
}
|
||
|
return int(val)
|
||
|
}
|
||
|
|
||
|
// RedisSetAdd 添加集合数据
|
||
|
func RedisSetAdd(key, val string) {
|
||
|
global.MG_REDIS.SAdd(key, val)
|
||
|
}
|
||
|
|
||
|
func RedisRename(key, newKey string) (string, error) {
|
||
|
result, err := global.MG_REDIS.Rename(key, newKey).Result()
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func RedisHashGet1(client *redis.Client, key, field string) string {
|
||
|
val, err := client.HGet(key, field).Result()
|
||
|
if err != nil {
|
||
|
return ""
|
||
|
}
|
||
|
return val
|
||
|
}
|
||
|
|
||
|
func RedisDel1(client *redis.Client, key ...string) (int64, error) {
|
||
|
result, err := client.Del(key...).Result()
|
||
|
if err != nil {
|
||
|
return -1, err
|
||
|
}
|
||
|
return result, err
|
||
|
}
|
||
|
|
||
|
func RedisBatchHashSet1(client *redis.Client, key string, fields map[string]interface{}) (error, string) {
|
||
|
val, err := client.HMSet(key, fields).Result()
|
||
|
if err != nil {
|
||
|
return err, ""
|
||
|
}
|
||
|
return nil, val
|
||
|
}
|
||
|
|
||
|
func RedisZAdd1(client *redis.Client, key string, values map[float64]interface{}) (int64, error) {
|
||
|
var members []redis.Z
|
||
|
for scale, value := range values {
|
||
|
members = append(members, redis.Z{
|
||
|
Score: scale,
|
||
|
Member: value,
|
||
|
})
|
||
|
}
|
||
|
result, err := client.ZAdd(key, members...).Result()
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func RedisZRange1(client *redis.Client, key string, start, stop int64, sort string) ([]interface{}, error) {
|
||
|
val := make([]interface{}, 0)
|
||
|
if sort == "asc" { // scale 正序
|
||
|
result, err := client.ZRangeWithScores(key, start, stop).Result()
|
||
|
if err != nil {
|
||
|
return val, err
|
||
|
}
|
||
|
for _, z := range result {
|
||
|
val = append(val, z.Member)
|
||
|
}
|
||
|
} else { // scale 倒序
|
||
|
result, err := client.ZRevRange(key, start, stop).Result()
|
||
|
if err != nil {
|
||
|
return val, err
|
||
|
}
|
||
|
for _, z := range result {
|
||
|
val = append(val, z)
|
||
|
}
|
||
|
return val, nil
|
||
|
}
|
||
|
|
||
|
return val, nil
|
||
|
}
|
||
|
|
||
|
func RedisZRemRangeByRank(client *redis.Client, key string, start, stop int64) error {
|
||
|
_, err := client.ZRemRangeByRank(key, start, stop).Result()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func RedisZCard1(client *redis.Client, key string) (int64, error) {
|
||
|
result, err := client.ZCard(key).Result()
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func RedisScan(client *redis.Client) ([]string, error) {
|
||
|
result, _, err := client.Scan(0, "", 0).Result()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|