|
@@ -8,6 +8,7 @@ import redis.clients.jedis.JedisCluster;
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
|
|
|
import static com.fire.dto.enums.RedisKey.PRIORITY_QUEUE;
|
|
|
|
|
@@ -26,15 +27,15 @@ public class RedisPriorityQueueScript {
|
|
|
public void initScript() {
|
|
|
String slotKey = PRIORITY_QUEUE.key();
|
|
|
//LUA脚本
|
|
|
- String SCRIPT = "local index = KEYS[1]"+
|
|
|
+ String SCRIPT = "local index = KEYS[1]" +
|
|
|
" local operating = ARGV[1]" +
|
|
|
" local score = ARGV[2]" +
|
|
|
" local content = ARGV[3]" +
|
|
|
" local res = 0" +
|
|
|
- " if operating == 'get' then"+
|
|
|
+ " if operating == 'get' then" +
|
|
|
" local ks=redis.call('ZRANGE', index, '0', '0')" +
|
|
|
" for _, item in ipairs(ks) do" +
|
|
|
- " res = redis.call('ZREM', index, item)"+
|
|
|
+ " res = redis.call('ZREM', index, item)" +
|
|
|
" if res > 0 then" +
|
|
|
" return item" +
|
|
|
" end" +
|
|
@@ -51,25 +52,35 @@ public class RedisPriorityQueueScript {
|
|
|
log.info("脚本已加载");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/*
|
|
|
* 获取优先级队列分发订单内容
|
|
|
* 返回 null:失败 获取数据:成功
|
|
|
*/
|
|
|
public String getContent() {
|
|
|
String priorityQueue = PRIORITY_QUEUE.key();
|
|
|
- Object res = jedisCluster.evalsha(runCode, Arrays.asList(priorityQueue), Arrays.asList("get","",""));
|
|
|
- return res.toString();
|
|
|
+ Object res = jedisCluster.evalsha(runCode, Collections.singletonList(priorityQueue), Arrays.asList("get", "", ""));
|
|
|
+ if (res != null) {
|
|
|
+ return res.toString();
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
/*
|
|
|
* 添加优先级队列分发订单内容
|
|
|
* 1.score 排序序号
|
|
|
* 2.content 分发记录类容
|
|
|
* 返回 0:覆盖 1:成功
|
|
|
*/
|
|
|
- public String addContent(Long score,String content) {
|
|
|
+ public Integer addContent(Long score, String content) {
|
|
|
String priorityQueue = PRIORITY_QUEUE.key();
|
|
|
- Object res = jedisCluster.evalsha(runCode, Arrays.asList(priorityQueue), Arrays.asList("add",String.valueOf(score),content));
|
|
|
- return res.toString();
|
|
|
+ Object res = jedisCluster.evalsha(runCode, Collections.singletonList(priorityQueue), Arrays.asList("add", String.valueOf(score), content));
|
|
|
+ if (res != null) {
|
|
|
+ return Integer.valueOf(res.toString());
|
|
|
+ } else {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|