实现签到功能
UserController类
/**
* 签到
* @return
*/
@PostMapping("/sign")
public Result sign(){
return userService.sign();
}
UserServiceImpl类
@Override
public Result sign() {
// 1.获取当前登录用户
Long userId = UserHolder.getUser().getId();
// 2.获取日期
LocalDateTime now = LocalDateTime.now();
String keySuffix = now.format(DateTimeFormatter.ofPattern(":yyMMMM"));
// 3.拼接key
String key=USER_SIGN_KEY+userId+keySuffix;
// 4.获取今天是本月的第几天
int dayOfMonth = now.getDayOfMonth();
// 5.写入redis setbit key offset 0/1
stringRedisTemplate.opsForValue().setBit(key,dayOfMonth-1,true);
return Result.ok();
}
统计连续签到
/**
* 统计连续签到天数
* @return
*/
@GetMapping("/sign/count")
public Result signCount(){
return userService.signCount();
}
@Override
public Result signCount() {
// 1.获取当前登录用户
Long userId = UserHolder.getUser().getId();
// 2.获取日期
LocalDateTime now = LocalDateTime.now();
String keySuffix = now.format(DateTimeFormatter.ofPattern(":yyyyMM"));
// 3.拼接key
String key = USER_SIGN_KEY + userId + keySuffix;
// 4.获取今天是本月的第几天
int dayOfMonth = now.getDayOfMonth();
// 5.获取本月截止今天为止的所有的签到记录,返回的是一个十进制的数字
// bitField sign:1010:202303 get u24 0
List<Long> result = stringRedisTemplate.opsForValue().bitField(key,
BitFieldSubCommands.create()
.get(BitFieldSubCommands.BitFieldType.unsigned(dayOfMonth)).valueAt(0));
if (result == null || result.isEmpty()) {
// 没有任何签到结果
return Result.ok(0);
}
Long num = result.get(0);
if (num == null || num == 0) {
return Result.ok(0);
}
// 6.循环遍历
int count = 0;
while (true) {
// 6.1.让每个数字与1做与运算,得到数字的最后一个bit位
// 6.2.判断这个bit为是否为0
if ((num & 1) == 0) {
// 6.3.如果为0,说明未签到,结束
break;
} else {
// 6.4.如果不为0,说明已签到,计数器+1
count++;
}
// 把数字右移一位,抛弃最后一个bit位,继续下一个bit位
num >>>= 1;
}
return Result.ok(count);
}
UV统计
@Test
void testHyperLogLog() {
String[] values = new String[1000];
int j;
for (int i = 0; i < 1000000; i++) {
j = i % 1000;
values[j] = "user_" + i;
if (j == 999) {
// 发送到Redis
stringRedisTemplate.opsForHyperLogLog().add("hp2", values);
}
}
//统计数量
Long count = stringRedisTemplate.opsForHyperLogLog().size("hp2");
System.out.println(count);
}