Skip to content

Commit a1dd57f

Browse files
committed
feta: 使用 HyperLogLog 存储UA以达到更好的性能
1 parent 8ef7401 commit a1dd57f

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

core/count.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@ import (
66
"github.com/soxft/busuanzi/config"
77
"github.com/soxft/busuanzi/library/tool"
88
"github.com/soxft/busuanzi/process/redisutil"
9+
"strings"
910
)
1011

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+
1118
// Count
1219
// @description return and count the number of users in the redis
1320
func Count(ctx context.Context, host string, path string, userIdentity string) (int64, int64, int64, int64) {
1421
_redis := redisutil.RDB
1522

1623
// 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))
1926

2027
redisPrefix := config.Redis.Prefix
2128

@@ -31,12 +38,13 @@ func Count(ctx context.Context, host string, path string, userIdentity string) (
3138
sitePv, _ := _redis.Incr(ctx, sitePvKey).Result()
3239
pagePv, _ := _redis.ZIncrBy(ctx, pagePvKey, 1, pathUnique).Result() // pagePv 使用 ZSet 存储
3340

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)
3744

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()
4048

4149
return sitePv, siteUv, int64(pagePv), pageUv
4250
}

0 commit comments

Comments
 (0)