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 GetKey(key string) string { result, err := client.Get(key).Result() if err == nil { return result } return "" }