|
|
|
@ -5,6 +5,7 @@ import ( |
|
|
|
"time" |
|
|
|
|
|
|
|
"github.com/flipped-aurora/gin-vue-admin/server/global" |
|
|
|
"github.com/go-redis/redis/v8" |
|
|
|
) |
|
|
|
|
|
|
|
//redis 基础设定
|
|
|
|
@ -12,6 +13,7 @@ type RedisStoreType struct { |
|
|
|
Expiration time.Duration |
|
|
|
PreKey string |
|
|
|
Context context.Context |
|
|
|
RedisDb *redis.Client |
|
|
|
} |
|
|
|
|
|
|
|
//启动redis
|
|
|
|
@ -21,6 +23,7 @@ func RunRedis() *RedisStoreType { |
|
|
|
redisStoreType.Expiration = time.Second * 300 |
|
|
|
redisStoreType.PreKey = global.GVA_CONFIG.RedisPrefix.PreFix + ":" |
|
|
|
redisStoreType.Context = context.Background() |
|
|
|
redisStoreType.RedisDb = global.GVA_REDIS |
|
|
|
return &redisStoreType |
|
|
|
// return &RedisStoreType{
|
|
|
|
// Expiration: time.Second * 300,
|
|
|
|
@ -29,6 +32,18 @@ func RunRedis() *RedisStoreType { |
|
|
|
// }
|
|
|
|
} |
|
|
|
|
|
|
|
//选择其他数据库
|
|
|
|
func (r *RedisStoreType) SetRedisDb(dbId int) { |
|
|
|
switch dbId { |
|
|
|
case 0: |
|
|
|
r.RedisDb = global.GVA_REDIS |
|
|
|
case 1: |
|
|
|
r.RedisDb = global.GVA_REDIS_WeChat |
|
|
|
default: |
|
|
|
r.RedisDb = global.GVA_REDIS |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//设置键前缀
|
|
|
|
func (r *RedisStoreType) SetRedisPrefix(prefix string) { |
|
|
|
r.PreKey = prefix |
|
|
|
@ -41,7 +56,7 @@ func (r *RedisStoreType) SetRedisTime(timeDate int64) { |
|
|
|
|
|
|
|
//设置字符串
|
|
|
|
func (r *RedisStoreType) Set(key string, value string) bool { |
|
|
|
err := global.GVA_REDIS.Set(r.Context, r.PreKey+key, value, r.Expiration).Err() |
|
|
|
err := r.RedisDb.Set(r.Context, r.PreKey+key, value, r.Expiration).Err() |
|
|
|
if err != nil { |
|
|
|
return false |
|
|
|
} |
|
|
|
@ -50,7 +65,7 @@ func (r *RedisStoreType) Set(key string, value string) bool { |
|
|
|
|
|
|
|
//获取字符串
|
|
|
|
func (r *RedisStoreType) Get(key string) (bool, string) { |
|
|
|
err := global.GVA_REDIS.Get(r.Context, r.PreKey+key) |
|
|
|
err := r.RedisDb.Get(r.Context, r.PreKey+key) |
|
|
|
if err.Err() != nil { |
|
|
|
return false, "" |
|
|
|
} |
|
|
|
@ -67,7 +82,7 @@ callback |
|
|
|
hashVal 获得值 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) HashGet(hashName, hashKey string) (errs bool, hashVal string) { |
|
|
|
err := global.GVA_REDIS.HGet(r.Context, r.PreKey+hashName, hashKey) |
|
|
|
err := r.RedisDb.HGet(r.Context, r.PreKey+hashName, hashKey) |
|
|
|
if err.Err() != nil { |
|
|
|
return false, "" |
|
|
|
} |
|
|
|
@ -81,14 +96,14 @@ func (r *RedisStoreType) HashGet(hashName, hashKey string) (errs bool, hashVal s |
|
|
|
@hashVal 要添加的值 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) HashSet(hashName, hashKey, hashVal string) bool { |
|
|
|
err := global.GVA_REDIS.HSet(r.Context, r.PreKey+hashName, hashKey, hashVal).Err() |
|
|
|
err := r.RedisDb.HSet(r.Context, r.PreKey+hashName, hashKey, hashVal).Err() |
|
|
|
if err != nil { |
|
|
|
return false |
|
|
|
} |
|
|
|
if r.Expiration == 0 { |
|
|
|
global.GVA_REDIS.Persist(r.Context, r.PreKey+hashName) |
|
|
|
r.RedisDb.Persist(r.Context, r.PreKey+hashName) |
|
|
|
} else { |
|
|
|
global.GVA_REDIS.PExpire(r.Context, r.PreKey+hashName, r.Expiration) |
|
|
|
r.RedisDb.PExpire(r.Context, r.PreKey+hashName, r.Expiration) |
|
|
|
} |
|
|
|
// global.GVA_REDIS.PExpire(r.Context, r.PreKey+hashName, r.Expiration)
|
|
|
|
return true |
|
|
|
@ -101,15 +116,15 @@ func (r *RedisStoreType) HashSet(hashName, hashKey, hashVal string) bool { |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) HashMsetAdd(hashName string, hashVal map[string]interface{}) bool { |
|
|
|
// rdb := RedisInit()
|
|
|
|
err := global.GVA_REDIS.HMSet(r.Context, r.PreKey+hashName, hashVal).Err() |
|
|
|
err := r.RedisDb.HMSet(r.Context, r.PreKey+hashName, hashVal).Err() |
|
|
|
// err := rdb.HMSet(ctx, "userfg", hashVal).Err()
|
|
|
|
if err != nil { |
|
|
|
return false |
|
|
|
} |
|
|
|
if r.Expiration == 0 { |
|
|
|
global.GVA_REDIS.Persist(r.Context, r.PreKey+hashName) |
|
|
|
r.RedisDb.Persist(r.Context, r.PreKey+hashName) |
|
|
|
} else { |
|
|
|
global.GVA_REDIS.PExpire(r.Context, r.PreKey+hashName, r.Expiration) |
|
|
|
r.RedisDb.PExpire(r.Context, r.PreKey+hashName, r.Expiration) |
|
|
|
} |
|
|
|
|
|
|
|
// fmt.Printf("错误sss=========》%v\n", hashVal)
|
|
|
|
@ -117,15 +132,15 @@ func (r *RedisStoreType) HashMsetAdd(hashName string, hashVal map[string]interfa |
|
|
|
} |
|
|
|
func (r *RedisStoreType) HashMsetAddAry(hashName string, hashVal []map[string]interface{}) bool { |
|
|
|
// rdb := RedisInit()
|
|
|
|
err := global.GVA_REDIS.HMSet(r.Context, r.PreKey+hashName, hashVal).Err() |
|
|
|
err := r.RedisDb.HMSet(r.Context, r.PreKey+hashName, hashVal).Err() |
|
|
|
// err := rdb.HMSet(ctx, "userfg", hashVal).Err()
|
|
|
|
if err != nil { |
|
|
|
return false |
|
|
|
} |
|
|
|
if r.Expiration == 0 { |
|
|
|
global.GVA_REDIS.Persist(r.Context, r.PreKey+hashName) |
|
|
|
r.RedisDb.Persist(r.Context, r.PreKey+hashName) |
|
|
|
} else { |
|
|
|
global.GVA_REDIS.PExpire(r.Context, r.PreKey+hashName, r.Expiration) |
|
|
|
r.RedisDb.PExpire(r.Context, r.PreKey+hashName, r.Expiration) |
|
|
|
} |
|
|
|
// global.GVA_REDIS.PExpire(r.Context, r.PreKey+hashName, r.Expiration)
|
|
|
|
// fmt.Printf("错误sss=========》%v\n", hashVal)
|
|
|
|
@ -139,7 +154,7 @@ func (r *RedisStoreType) HashMsetAddAry(hashName string, hashVal []map[string]in |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) HashGetAll(hashName string) (map[string]string, bool) { |
|
|
|
// rdb := RedisInit()
|
|
|
|
val, err := global.GVA_REDIS.HGetAll(r.Context, r.PreKey+hashName).Result() |
|
|
|
val, err := r.RedisDb.HGetAll(r.Context, r.PreKey+hashName).Result() |
|
|
|
if err != nil { |
|
|
|
return val, false |
|
|
|
} |
|
|
|
@ -148,3 +163,192 @@ func (r *RedisStoreType) HashGetAll(hashName string) (map[string]string, bool) { |
|
|
|
} |
|
|
|
return val, true |
|
|
|
} |
|
|
|
|
|
|
|
//Redis 列表(List)
|
|
|
|
|
|
|
|
/** |
|
|
|
Linsert 命令用于在列表的元素前或者后插入元素。当指定元素不存在于列表中时,不执行任何操作。 |
|
|
|
当列表不存在时,被视为空列表,不执行任何操作。 |
|
|
|
如果 key 不是列表类型,返回一个错误。 |
|
|
|
@key 列表 |
|
|
|
@op 插入状态 (1:在pivot之后;2:在pivot之前) |
|
|
|
@pivot 定位值 |
|
|
|
@value 要插入值 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Linsert(key string, op int, pivot, value interface{}) (int64, bool) { |
|
|
|
BeforeOrAfter := "BEFORE" |
|
|
|
if op != 1 { |
|
|
|
BeforeOrAfter = "AFTER" |
|
|
|
} |
|
|
|
linsert, linsertErr := r.RedisDb.LInsert(r.Context, key, BeforeOrAfter, pivot, value).Result() |
|
|
|
if linsertErr != nil { |
|
|
|
return 0, false |
|
|
|
} |
|
|
|
return linsert, true |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Lindex 命令用于通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。 |
|
|
|
@key 列表 |
|
|
|
@index 索引 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Lindex(key string, index int64) (val string, err error) { |
|
|
|
val, err = r.RedisDb.LIndex(r.Context, key, index).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Llen 命令用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误。 |
|
|
|
@key 列表 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Llen(key string) (val int64, err error) { |
|
|
|
val, err = r.RedisDb.LLen(r.Context, key).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Lpop 命令用于移除并返回列表的第一个元素。 |
|
|
|
@key 列表 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Lpop(key string) (val string, err error) { |
|
|
|
val, err = r.RedisDb.LPop(r.Context, key).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误。 |
|
|
|
@key 列表 |
|
|
|
@value 要插入的字符串 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Lpush(key string, value ...interface{}) (val int64, err error) { |
|
|
|
val, err = r.RedisDb.LPush(r.Context, key, value).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Lpushx 将一个值插入到已存在的列表头部,列表不存在时操作无效。 |
|
|
|
@key 列表 |
|
|
|
@value 要插入的字符串 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Lpushx(key, value string) (val int64, err error) { |
|
|
|
val, err = r.RedisDb.LPushX(r.Context, key, value).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Lrange 返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。 |
|
|
|
@key 列表 |
|
|
|
@start 起始值 |
|
|
|
@stop 结束值 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Lrange(key string, start, stop int64) (val []string, err error) { |
|
|
|
val, err = r.RedisDb.LRange(r.Context, key, start, stop).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Lrem 根据参数 COUNT 的值,移除列表中与参数 VALUE 相等的元素。 |
|
|
|
COUNT 的值可以是以下几种: |
|
|
|
count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。 |
|
|
|
count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。 |
|
|
|
count = 0 : 移除表中所有与 VALUE 相等的值。 |
|
|
|
@start = COUNT |
|
|
|
@key 列表 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Lrem(key string, start int64, value ...interface{}) (val int64, err error) { |
|
|
|
val, err = r.RedisDb.LRem(r.Context, key, start, value).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Redis Lset 通过索引来设置元素的值。 |
|
|
|
|
|
|
|
当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误。 |
|
|
|
@key 列表 |
|
|
|
@indexes 索引值 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Lset(key string, indexes int64, value ...interface{}) (val string, err error) { |
|
|
|
val, err = r.RedisDb.LSet(r.Context, key, indexes, value).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。 |
|
|
|
|
|
|
|
下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推。 |
|
|
|
@key 列表 |
|
|
|
@start 起始值 |
|
|
|
@stop 结束值 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Ltrim(key string, start, stop int64) (val string, err error) { |
|
|
|
val, err = r.RedisDb.LTrim(r.Context, key, start, stop).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Rpop 命令用于移除列表的最后一个元素,返回值为移除的元素。 |
|
|
|
@key 列表 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Rpop(key string) (val string, err error) { |
|
|
|
val, err = r.RedisDb.RPop(r.Context, key).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Rpoplpush 命令用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回。 |
|
|
|
@sourceKey 源列表 |
|
|
|
@newKey 目标列表 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Rpoplpush(sourceKey, newKey string) (val string, err error) { |
|
|
|
val, err = r.RedisDb.RPopLPush(r.Context, sourceKey, newKey).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Rpush 命令用于将一个或多个值插入到列表的尾部(最右边)。 |
|
|
|
如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。 |
|
|
|
@key 列表 |
|
|
|
@value 要插入的字符串 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Rpush(key string, value ...interface{}) (val int64, err error) { |
|
|
|
val, err = r.RedisDb.RPush(r.Context, key, value).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效。 |
|
|
|
@key 列表 |
|
|
|
@value 要插入的字符串 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Rpushx(key string, value ...interface{}) (val int64, err error) { |
|
|
|
val, err = r.RedisDb.RPushX(r.Context, key, value).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Blpop 命令移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 |
|
|
|
@key 列表 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Blpop(key string) (val []string, err error) { |
|
|
|
val, err = r.RedisDb.BLPop(r.Context, r.Expiration, key).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Brpop 命令移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 |
|
|
|
@key 列表 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Brpop(key string) (val []string, err error) { |
|
|
|
val, err = r.RedisDb.BRPop(r.Context, r.Expiration, key).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
Brpoplpush 命令从列表中取出最后一个元素,并插入到另外一个列表的头部; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 |
|
|
|
@source 源列表 |
|
|
|
@destination 目标列表 |
|
|
|
*/ |
|
|
|
func (r *RedisStoreType) Brpoplpush(source, destination string) (val string, err error) { |
|
|
|
val, err = r.RedisDb.BRPopLPush(r.Context, source, destination, r.Expiration).Result() |
|
|
|
return |
|
|
|
} |
|
|
|
|