/
user.go
106 lines (89 loc) · 2.63 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Copyright (c) 2014-2015, GoBelieve
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import "fmt"
import "time"
import log "github.com/golang/glog"
import "github.com/garyburd/redigo/redis"
import "errors"
func GetUserForbidden(appid int64, uid int64) (int, error) {
conn := redis_pool.Get()
defer conn.Close()
key := fmt.Sprintf("users_%d_%d", appid, uid)
forbidden, err := redis.Int(conn.Do("HGET", key, "forbidden"))
if err != nil {
log.Info("hget error:", err)
return 0, err
}
return forbidden, nil
}
func LoadUserAccessToken(token string) (int64, int64, error) {
conn := redis_pool.Get()
defer conn.Close()
key := fmt.Sprintf("access_token_%s", token)
var uid int64
var appid int64
exists, err := redis.Bool(conn.Do("EXISTS", key))
if err != nil {
return 0, 0, err
}
if !exists {
return 0, 0, errors.New("token non exists")
}
reply, err := redis.Values(conn.Do("HMGET", key, "user_id", "app_id"))
if err != nil {
log.Info("hmget error:", err)
return 0, 0, err
}
_, err = redis.Scan(reply, &uid, &appid)
if err != nil {
log.Warning("scan error:", err)
return 0, 0, err
}
return appid, uid, nil
}
func CountUser(appid int64, uid int64) {
conn := redis_pool.Get()
defer conn.Close()
key := fmt.Sprintf("statistics_users_%d", appid)
_, err := conn.Do("PFADD", key, uid)
if err != nil {
log.Info("pfadd err:", err)
}
}
func CountDAU(appid int64, uid int64) {
conn := redis_pool.Get()
defer conn.Close()
now := time.Now()
date := fmt.Sprintf("%d_%d_%d", now.Year(), int(now.Month()), now.Day())
key := fmt.Sprintf("statistics_dau_%s_%d", date, appid)
_, err := conn.Do("PFADD", key, uid)
if err != nil {
log.Info("pfadd err:", err)
}
}
func SetUserUnreadCount(appid int64, uid int64, count int32) {
conn := redis_pool.Get()
defer conn.Close()
key := fmt.Sprintf("users_%d_%d", appid, uid)
_, err := conn.Do("HSET", key, "unread", count)
if err != nil {
log.Info("hset err:", err)
}
}