@@ -6,16 +6,23 @@ import (
6
6
"github.com/soxft/busuanzi/config"
7
7
"github.com/soxft/busuanzi/library/tool"
8
8
"github.com/soxft/busuanzi/process/redisutil"
9
+ "strings"
9
10
)
10
11
12
+ //index 数据类型 key
13
+ //sitePv string bsz:site_pv:md5(example.com)
14
+ //siteUv HyperLogLog bsz:site_uv:md5(example.com)
15
+ //pagePv zset bsz:page_pv:md5(example.com)
16
+ //pageUv HyperLogLog bsz:site_uv:md5(example.com):md5(example.com&index.html)
17
+
11
18
// Count
12
19
// @description return and count the number of users in the redis
13
20
func Count (ctx context.Context , host string , path string , userIdentity string ) (int64 , int64 , int64 , int64 ) {
14
21
_redis := redisutil .RDB
15
22
16
23
// encode
17
- var pathUnique = tool .Md5 (host + "&" + path )
18
- var siteUnique = tool .Md5 (host )
24
+ var pathUnique = strings . ToLower ( tool .Md5 (host + "&" + path ) )
25
+ var siteUnique = strings . ToLower ( tool .Md5 (host ) )
19
26
20
27
redisPrefix := config .Redis .Prefix
21
28
@@ -31,12 +38,13 @@ func Count(ctx context.Context, host string, path string, userIdentity string) (
31
38
sitePv , _ := _redis .Incr (ctx , sitePvKey ).Result ()
32
39
pagePv , _ := _redis .ZIncrBy (ctx , pagePvKey , 1 , pathUnique ).Result () // pagePv 使用 ZSet 存储
33
40
34
- // siteUv 和 pageUv 使用 Set 存储
35
- _ , _ = _redis .SAdd (ctx , siteUvKey , userIdentity ). Result ( )
36
- _ , _ = _redis .SAdd (ctx , pageUvKey , userIdentity ). Result ( )
41
+ // siteUv 和 pageUv 使用 HyperLogLog 存储
42
+ _redis .PFAdd (ctx , siteUvKey , userIdentity )
43
+ _redis .PFAdd (ctx , pageUvKey , userIdentity )
37
44
38
- siteUv , _ := _redis .SCard (ctx , siteUvKey ).Result ()
39
- pageUv , _ := _redis .SCard (ctx , pageUvKey ).Result ()
45
+ // count siteUv and pageUv
46
+ siteUv , _ := _redis .PFCount (ctx , siteUvKey ).Result ()
47
+ pageUv , _ := _redis .PFCount (ctx , pageUvKey ).Result ()
40
48
41
49
return sitePv , siteUv , int64 (pagePv ), pageUv
42
50
}
0 commit comments