redis.go
1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package redisdata
import (
"encoding/json"
"errors"
"fmt"
"oppmg/common/log"
"oppmg/common/redis"
"oppmg/protocol"
"time"
)
func SetLoginToken(param protocol.LoginAuthToken, userid int64) error {
client := redis.GetRedis()
var (
key string
value []byte
err error
exp int64
nowTime = time.Now().Unix()
)
exp = param.RefreshExpires - nowTime
key = fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid)
value, _ = json.Marshal(param)
err = client.Set(key, string(value), time.Duration(exp)*time.Second).Err()
return err
}
func GetLoginToken(userid int64) (protocol.LoginAuthToken, error) {
client := redis.GetRedis()
var (
key string
value string
err error
result protocol.LoginAuthToken
)
key = fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid)
value = client.Get(key).String()
if len(value) == 0 {
return result, errors.New("Token not found")
}
err = json.Unmarshal([]byte(value), &result)
if err != nil {
log.Error("Unmarshal redis value:%s err:%s", value, err)
return result, err
}
return result, nil
}
func ExistLoginToken(userid int64) bool {
client := redis.GetRedis()
key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid)
value := client.Exists(key).Val()
if value > 0 {
return true
}
return false
}