RedisDelayQueue.java
1.2 KB
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
package cn.com.glsx.stdinterface.modules.queue;
import com.glsx.plat.redis.utils.RedisUtils;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;
/**
* redis延时队列存储
* @author xiangyanlin
* @date 2022/9/9
*/
@Component
public class RedisDelayQueue {
@Autowired
private RedisUtils redisUtils;
public boolean add(String taskKey, String businessNo, Long timestamp) {
return redisUtils.zsSet(taskKey, businessNo, timestamp);
}
public Collection<String> getAndRemoveBusinessNo(String taskKey) {
Set<String> result = Sets.newHashSet();
Set<String> values = redisUtils.zsRangeByScore(taskKey, 0, System.currentTimeMillis());
if (values.isEmpty()) {
return result;
}
for (String value : values) {
if (Objects.isNull(value)) {
continue;
}
result.add(value);
redisUtils.zSetRemove(taskKey, value);
}
return result;
}
}