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条,符合概率预期

再次查看内存消耗

相关推荐
h79971020 分钟前
redis lua脚本(go)调用教程以及debug调试
redis·golang·lua
虫师c4 小时前
分布式缓存实战:Redis集群与性能优化
redis·分布式·缓存·redis集群·高可用架构·生产环境·数据分片
我真的是大笨蛋16 小时前
Redis的String详解
java·数据库·spring boot·redis·spring·缓存
zhengzizhe18 小时前
Redssion出现attempt to unlock lock, not locked by current thread by node id
redis
兜兜风d'1 天前
redis字符串命令
数据库·redis·缓存
西瓜er1 天前
Docker 一键部署指南:GitLab、Nacos、Redis、MySQL 与 MinIO 全解析
redis·docker·gitlab
道可到1 天前
别再瞎拼技术栈!Postgres 已经能干 Redis 的活了
redis·后端·postgresql
野犬寒鸦1 天前
从零起步学习Redis || 第十二章:Redis Cluster集群如何解决Redis单机模式的性能瓶颈及高可用分布式部署方案详解
java·数据库·redis·后端·缓存
为java加瓦1 天前
优化 Service 层架构:从高耦合到清晰分层的实战重构指南
log4j
悟能不能悟1 天前
redis的红锁
数据库·redis·缓存