Redis实战案例27-UV统计

1. Redis的HyperLogLog的统计功能


示例:

表明HyperLogLog不管加入重复元素多少次都不会让count++,不会计数重复元素,所以适合做UV计数

2. 简单实现UV测试

通过单元测试,向 HyperLogLog 中添加 100 万条数据,看看内存占用和统计效果如何

java 复制代码
/**
 * UV统计测试
 */
@Test
void testHyperLogLog() {
    String[] values = new String[1000];
    int j = 0;
    //分批·每次一千导入到Redis中
    for (int i = 0; i < 1000000; i++) {
        j = i % 1000;
        values[j] = "user_" + i;
        if(j == 999) {
            // 发送到Redis中
            stringRedisTemplate.opsForHyperLogLog().add("hl1", values);
        }
    }
    // 统计数量
    Long count = stringRedisTemplate.opsForHyperLogLog().size("hl1");
    System.out.println("count = " + count);
}

插入了一百万条,最后得到的是997573条,符合概率预期

再次查看内存消耗

相关推荐
小小工匠2 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
taocarts_bidfans2 天前
反向海淘跨境缓存架构优化:taocarts Redis分层缓存实战技术
redis·缓存·架构·反向海淘·taocarts
炘爚2 天前
Linux——Redis
数据库·redis·缓存
csjane10792 天前
Redisson 限流原理
java·redis
ThanksGive2 天前
Go 服务里的 Redis 锁惊群问题:一次本地合流优化实践
redis
小挪号底迪滴2 天前
Redis 和 MySQL 数据不一致怎么办?缓存更新策略实战
redis·mysql·缓存
闪电悠米2 天前
黑马点评-Redis ZSet-实现关注 Feed 流
服务器·网络·数据库·redis·缓存·junit·lua
Devin~Y2 天前
大厂 Java 面试实录:从音视频内容社区到 AI RAG 的全链路技术设计
java·spring boot·redis·spring cloud·微服务·kafka·音视频
小小工匠2 天前
Redis - 主从集群脑裂:数据丢失的隐藏杀手
数据库·redis