说明

  • 使用 redis 的 Bitmap 存储用户签到信息

核心代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
@Service
public class BitmapCheckInService {

@Autowired
private StringRedisTemplate redisTemplate;

/**
* 用户签到
*/
public boolean checkIn(Long userId) {
LocalDate today = LocalDate.now();
int day = today.getDayOfMonth(); // 获取当月的第几天
String key = buildSignKey(userId, today);

// 检查今天是否已经签到
Boolean isSigned = redisTemplate.opsForValue().getBit(key, day - 1);
if (isSigned != null && isSigned) {
return false; // 已经签到
}

// 设置签到标记
redisTemplate.opsForValue().setBit(key, day - 1, true);

// 设置过期时间(确保数据不会永久保存)
redisTemplate.expire(key, 100, TimeUnit.DAYS);

// 更新连续签到记录
updateContinuousSignDays(userId);

return true;
}

/**
* 更新连续签到天数
*/
private void updateContinuousSignDays(Long userId) {
LocalDate today = LocalDate.now();
String continuousKey = "user:sign:continuous:" + userId;

// 判断昨天是否签到
boolean yesterdayChecked = isSignedIn(userId, today.minusDays(1));

if (yesterdayChecked) {
// 昨天签到了,连续签到天数+1
redisTemplate.opsForValue().increment(continuousKey);
} else {
// 昨天没签到,重置连续签到天数为1
redisTemplate.opsForValue().set(continuousKey, "1");
}
}

/**
* 判断用户指定日期是否签到
*/
public boolean isSignedIn(Long userId, LocalDate date) {
int day = date.getDayOfMonth();
String key = buildSignKey(userId, date);

Boolean isSigned = redisTemplate.opsForValue().getBit(key, day - 1);
return isSigned != null && isSigned;
}

/**
* 获取用户连续签到天数
*/
public int getContinuousSignDays(Long userId) {
String continuousKey = "user:sign:continuous:" + userId;
String value = redisTemplate.opsForValue().get(continuousKey);
return value != null ? Integer.parseInt(value) : 0;
}

/**
* 获取用户当月签到次数
*/
public long getMonthSignCount(Long userId, LocalDate date) {
String key = buildSignKey(userId, date);
int dayOfMonth = date.lengthOfMonth(); // 当月总天数

return redisTemplate.execute((RedisCallback<Long>) con -> {
return con.bitCount(key.getBytes());
});
}

/**
* 获取用户当月签到情况
*/
public List<Integer> getMonthSignData(Long userId, LocalDate date) {
List<Integer> result = new ArrayList<>();
String key = buildSignKey(userId, date);
int dayOfMonth = date.lengthOfMonth(); // 当月总天数

for (int i = 0; i < dayOfMonth; i++) {
Boolean isSigned = redisTemplate.opsForValue().getBit(key, i);
result.add(isSigned != null && isSigned ? 1 : 0);
}

return result;
}

/**
* 获取用户当月首次签到时间
*/
public int getFirstSignDay(Long userId, LocalDate date) {
String key = buildSignKey(userId, date);
int dayOfMonth = date.lengthOfMonth(); // 当月总天数

for (int i = 0; i < dayOfMonth; i++) {
Boolean isSigned = redisTemplate.opsForValue().getBit(key, i);
if (isSigned != null && isSigned) {
return i + 1; // 返回第一次签到的日期
}
}

return -1; // 本月没有签到记录
}

/**
* 构建签到Key
*/
private String buildSignKey(Long userId, LocalDate date) {
return String.format("user:sign:%d:%d%02d", userId, date.getYear(), date.getMonthValue());
}
}