Skip to content

Commit

Permalink
feta: 使用 HyperLogLog 存储UA以达到更好的性能
Browse files Browse the repository at this point in the history
  • Loading branch information
soxft committed Jul 31, 2024
1 parent 8ef7401 commit a1dd57f
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions core/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@ import (
"github.com/soxft/busuanzi/config"
"github.com/soxft/busuanzi/library/tool"
"github.com/soxft/busuanzi/process/redisutil"
"strings"
)

//index 数据类型 key
//sitePv string bsz:site_pv:md5(example.com)
//siteUv HyperLogLog bsz:site_uv:md5(example.com)
//pagePv zset bsz:page_pv:md5(example.com)
//pageUv HyperLogLog bsz:site_uv:md5(example.com):md5(example.com&index.html)

// Count
// @description return and count the number of users in the redis
func Count(ctx context.Context, host string, path string, userIdentity string) (int64, int64, int64, int64) {
_redis := redisutil.RDB

// encode
var pathUnique = tool.Md5(host + "&" + path)
var siteUnique = tool.Md5(host)
var pathUnique = strings.ToLower(tool.Md5(host + "&" + path))
var siteUnique = strings.ToLower(tool.Md5(host))

redisPrefix := config.Redis.Prefix

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

// siteUv 和 pageUv 使用 Set 存储
_, _ = _redis.SAdd(ctx, siteUvKey, userIdentity).Result()
_, _ = _redis.SAdd(ctx, pageUvKey, userIdentity).Result()
// siteUv 和 pageUv 使用 HyperLogLog 存储
_redis.PFAdd(ctx, siteUvKey, userIdentity)
_redis.PFAdd(ctx, pageUvKey, userIdentity)

siteUv, _ := _redis.SCard(ctx, siteUvKey).Result()
pageUv, _ := _redis.SCard(ctx, pageUvKey).Result()
// count siteUv and pageUv
siteUv, _ := _redis.PFCount(ctx, siteUvKey).Result()
pageUv, _ := _redis.PFCount(ctx, pageUvKey).Result()

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

0 comments on commit a1dd57f

Please sign in to comment.