redis, updatediff

This commit is contained in:
suyl
2021-07-07 15:24:12 +08:00
parent 2beca9fbcb
commit acb48212a1
7 changed files with 346 additions and 19 deletions

46
utils/redis.go Normal file
View File

@@ -0,0 +1,46 @@
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
}