From a1dd57f0606c1bff19b50da2b9bce4a432860e5b Mon Sep 17 00:00:00 2001 From: soxft Date: Wed, 31 Jul 2024 10:23:27 +0800 Subject: [PATCH] =?UTF-8?q?feta:=20=E4=BD=BF=E7=94=A8=20HyperLogLog=20?= =?UTF-8?q?=E5=AD=98=E5=82=A8UA=E4=BB=A5=E8=BE=BE=E5=88=B0=E6=9B=B4?= =?UTF-8?q?=E5=A5=BD=E7=9A=84=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/count.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/core/count.go b/core/count.go index 7b6e8a1..8683b82 100644 --- a/core/count.go +++ b/core/count.go @@ -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 @@ -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 }