-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add UniqueVisitor * add test * update readme
- Loading branch information
Showing
7 changed files
with
98 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package redis | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"math" | ||
"testing" | ||
|
||
"github.com/cloudwego/hertz/pkg/common/test/assert" | ||
) | ||
|
||
func TestHyperLogLog(t *testing.T) { | ||
Init() | ||
values := make([]interface{}, 1000) | ||
ctx := context.Background() | ||
var total int64 = 1000000 | ||
// 批量保存 100w 条用户记录,每批 1000 条 | ||
var i int64 | ||
for i = 0; i < total; i++ { | ||
// 获取当前批次的索引 | ||
j := i % 1000 | ||
// 生成用户记录 | ||
values[j] = "user_" + fmt.Sprint(i) | ||
// 每 1000 条记录发送一次到 Redis | ||
if j == 999 { | ||
err := RedisClient.PFAdd(ctx, "hl2", values...).Err() | ||
if err != nil { | ||
log.Fatalf("Failed to add values to HyperLogLog: %v", err) | ||
} | ||
} | ||
} | ||
|
||
// 统计 HyperLogLog 中的用户数量 | ||
count, err := RedisClient.PFCount(ctx, "hl2").Result() | ||
if err != nil { | ||
log.Fatalf("Failed to get HyperLogLog count: %v", err) | ||
} | ||
log.Printf("HyperLogLog count: %d", count) | ||
diff := math.Abs(float64(total - count)) | ||
assert.True(t, count > 0) | ||
assert.True(t, diff < float64(total)/100) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package interceptor | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
"xzdp/biz/dal/redis" | ||
"xzdp/biz/pkg/constants" | ||
"xzdp/biz/utils" | ||
|
||
"github.com/cloudwego/hertz/pkg/app" | ||
"github.com/cloudwego/hertz/pkg/common/hlog" | ||
) | ||
|
||
func UniqueVisitor(ctx context.Context, c *app.RequestContext) { | ||
hlog.CtxInfof(ctx, "Unique Visitor") | ||
userDTO := utils.GetUser(ctx) | ||
if userDTO == nil { | ||
c.Next(ctx) | ||
return | ||
} | ||
hlog.CtxInfof(ctx, "Unique Visitor userDTO: %v", userDTO) | ||
now := time.Now() | ||
|
||
// 格式化为 YYYY-MM-DD 格式 | ||
today := now.Format("2006-01-02") | ||
hhlVal := fmt.Sprint(userDTO.ID) + today | ||
err := redis.RedisClient.PFAdd(ctx, constants.HLL_UV_KEY, hhlVal) | ||
if err != nil { | ||
hlog.CtxErrorf(ctx, "PFAdd error: %v", err) | ||
} | ||
c.Next(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters