-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.go
69 lines (63 loc) · 1.58 KB
/
user.go
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
59
60
61
62
63
64
65
66
67
68
69
package accs
import (
"time"
"github.com/garyburd/redigo/redis"
"github.com/grunmax/GinRedisApi/domn"
"github.com/grunmax/GinRedisApi/util"
"github.com/patrickmn/go-cache"
)
func UserCreate(item domn.UserSignupForm, pool *redis.Pool) (string, error) {
c := pool.Get()
defer c.Close()
item.Id = util.NewId()
hash := util.GetMD5Hash(item.Password)
key := "user:" + hash
if _, err := c.Do("HMSET", key, "id", item.Id, "email", item.Email); err != nil {
util.Log("User create error", err)
return "", err
} else {
util.Log("hash", hash)
return hash, nil
}
}
func UserCheck(item domn.UserLoginForm, pool *redis.Pool) (bool, string, error) {
c := pool.Get()
defer c.Close()
hash := util.GetMD5Hash(item.Password)
key := "user:" + hash
email, err := redis.String(c.Do("HGET", key, "email"))
if err != nil {
util.Log("user check error", err)
return false, "", err
}
return email == item.Email, hash, nil
}
func KeyExists(hash string, pool *redis.Pool) (bool, error) {
c := pool.Get()
defer c.Close()
key := "user:" + hash
exist, err := redis.Bool(c.Do("EXISTS", key))
if err != nil {
util.Log("check todo key error", err)
return false, err
}
return exist, nil
}
func KeyExistsCached(hash string, che *cache.Cache, pool *redis.Pool) (bool, error) {
_, isexist := che.Get(hash)
if isexist {
return true, nil
}
c := pool.Get()
defer c.Close()
key := "user:" + hash
exist, err := redis.Bool(c.Do("EXISTS", key))
if err != nil {
util.Log("check todo key error", err)
return false, err
}
if exist {
che.Set(hash, "foo", 10*time.Minute)
}
return exist, nil
}