package com.example.redis;
import jakarta.annotation.PostConstruct;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@SpringBootTest
public class HyperLogLog {
@Autowired
private RedisTemplate redisTemplate;
@PostConstruct // 模拟不同来自ip的点击
public void init() {
System.out.println("HyperLogLog init");
new Thread(() -> {
String ip = null;
for (int i = 0; i < 200; i++) {
Random random = new Random();
ip = random.nextInt(256) + "." +
random.nextInt(256) + "." +
random.nextInt(256) + "." +
random.nextInt(256);
redisTemplate.opsForHyperLogLog().add("ip", ip);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "HyperLogLog").start();
}
public long uv() {
return redisTemplate.opsForHyperLogLog().size("ip");
}
@Test
public void test() {
while (true) {
try {
TimeUnit.SECONDS.sleep(3);
System.out.println(uv());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Redis基于HyperLogLog的基数统计
最新推荐文章于 2024-09-27 11:02:32 发布