dddd
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.

136 lines
3.4 KiB

package redishandel
import (
"context"
"time"
"github.com/flipped-aurora/gin-vue-admin/server/global"
)
//redis 基础设定
type RedisStoreType struct {
Expiration time.Duration
PreKey string
Context context.Context
}
//启动redis
func RunRedis() *RedisStoreType {
var redisStoreType RedisStoreType
// contextBackground := context.Background()
redisStoreType.Expiration = time.Second * 300
redisStoreType.PreKey = global.GVA_CONFIG.RedisPrefix.PreFix + ":"
redisStoreType.Context = context.Background()
return &redisStoreType
// return &RedisStoreType{
// Expiration: time.Second * 300,
// PreKey: global.GVA_CONFIG.RedisPrefix.PreFix + ":",
// Context: contextBackground
// }
}
//设置键前缀
func (r *RedisStoreType) SetRedisPrefix(prefix string) {
r.PreKey = prefix
}
//设置过期时间
func (r *RedisStoreType) SetRedisTime(timeDate int64) {
r.Expiration = time.Second * time.Duration(timeDate)
}
//设置字符串
func (r *RedisStoreType) Set(key string, value string) bool {
err := global.GVA_REDIS.Set(r.Context, r.PreKey+key, value, r.Expiration).Err()
if err != nil {
return false
}
return true
}
//获取字符串
func (r *RedisStoreType) Get(key string) (bool, string) {
err := global.GVA_REDIS.Get(r.Context, r.PreKey+key)
if err.Err() != nil {
return false, ""
}
return true, err.Val()
}
//哈希操作
/*
获取单个哈希键值
@hashName 集合名称
@hashKey 哈希键
callback
errs 状态
hashVal 获得值
*/
func (r *RedisStoreType) HashGet(hashName, hashKey string) (errs bool, hashVal string) {
err := global.GVA_REDIS.HGet(r.Context, r.PreKey+hashName, hashKey)
if err.Err() != nil {
return false, ""
}
return true, err.Val()
}
/*
为哈希表中的字段赋值 单一设置
@hashName 集合名称
@hashKey 哈希键
@hashVal 要添加的值
*/
func (r *RedisStoreType) HashSet(hashName, hashKey, hashVal string) bool {
err := global.GVA_REDIS.HSet(r.Context, r.PreKey+hashName, hashKey, hashVal).Err()
if err != nil {
return false
}
global.GVA_REDIS.PExpire(r.Context, r.PreKey+hashName, r.Expiration)
return true
}
/*
同时将多个 field-value (字段-)对设置到哈希表中
@hashName 集合名称
@hashVal 要添加的键与值
*/
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 := rdb.HMSet(ctx, "userfg", hashVal).Err()
if err != nil {
return false
}
global.GVA_REDIS.PExpire(r.Context, r.PreKey+hashName, r.Expiration)
// fmt.Printf("错误sss=========》%v\n", hashVal)
return true
}
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 := rdb.HMSet(ctx, "userfg", hashVal).Err()
if err != nil {
return false
}
global.GVA_REDIS.PExpire(r.Context, r.PreKey+hashName, r.Expiration)
// fmt.Printf("错误sss=========》%v\n", hashVal)
return true
}
/*
返回哈希表中所有的字段和值
@hashName 集合名称
@hashKey 哈希键
*/
func (r *RedisStoreType) HashGetAll(hashName string) (map[string]string, bool) {
// rdb := RedisInit()
val, err := global.GVA_REDIS.HGetAll(r.Context, r.PreKey+hashName).Result()
if err != nil {
return val, false
}
if len(val) == 0 {
return val, false
}
return val, true
}