Files
jx-print/putils/redis.go
邹宗楠 66db5867a4 1
2022-08-25 16:10:01 +08:00

49 lines
978 B
Go

package putils
import (
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-print/globals"
"github.com/go-redis/redis"
"time"
)
const (
DefTokenDuration = time.Hour * 24 * 7
)
var (
client *redis.Client
)
func init() {
globals.SugarLogger.Debugf("redis init..")
client = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 5,
PoolSize: 50,
})
pong, err := client.Ping().Result()
globals.SugarLogger.Debugf("redis pong %v, err: %v", pong, err)
}
func SetKey(key string, value interface{}, expiration time.Duration) error {
strValue := string(utils.MustMarshal(value))
return client.Set(key, strValue, expiration).Err()
}
func DelKey(key string) error {
return client.Del(key).Err()
}
func GetKey(key string) interface{} {
result, err := client.Get(key).Result()
if err == nil {
var retVal interface{}
if err = utils.UnmarshalUseNumber([]byte(result), &retVal); err == nil {
return retVal
}
}
return nil
}