47 lines
946 B
Go
47 lines
946 B
Go
package utils
|
|
|
|
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: "",
|
|
})
|
|
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 GetKet(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
|
|
}
|