|
@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.logging.log4j.util.Strings;
|
|
import org.apache.logging.log4j.util.Strings;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import redis.clients.jedis.JedisCluster;
|
|
import redis.clients.jedis.JedisCluster;
|
|
|
|
+import redis.clients.jedis.exceptions.JedisNoScriptException;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.annotation.PostConstruct;
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
@@ -15,6 +16,19 @@ import static com.fire.dto.enums.RedisKey.ORDER_DOWN;
|
|
@Slf4j
|
|
@Slf4j
|
|
@Component
|
|
@Component
|
|
public class RedisOrderIdScript {
|
|
public class RedisOrderIdScript {
|
|
|
|
+
|
|
|
|
+ //LUA脚本
|
|
|
|
+ private final static String SCRIPT = "local key = KEYS[1]" +
|
|
|
|
+ " local customerOrder = KEYS[2]" +
|
|
|
|
+ " local orderId = ARGV[1]" +
|
|
|
|
+ " local res = redis.pcall('HEXISTS', key, customerOrder)" +
|
|
|
|
+ " if res == 1 then" +
|
|
|
|
+ " return 0" +
|
|
|
|
+ " else" +
|
|
|
|
+ " redis.pcall('HSET', key, customerOrder, orderId)" +
|
|
|
|
+ " return 1" +
|
|
|
|
+ " end";
|
|
|
|
+
|
|
@Resource
|
|
@Resource
|
|
private JedisCluster jedisCluster;
|
|
private JedisCluster jedisCluster;
|
|
|
|
|
|
@@ -25,24 +39,17 @@ public class RedisOrderIdScript {
|
|
*/
|
|
*/
|
|
@PostConstruct
|
|
@PostConstruct
|
|
public void initScript() {
|
|
public void initScript() {
|
|
- String slotKey = ORDER_DOWN.key();
|
|
|
|
- //LUA脚本
|
|
|
|
- String SCRIPT = "local key = KEYS[1]" +
|
|
|
|
- " local customerOrder = KEYS[2]" +
|
|
|
|
- " local orderId = ARGV[1]" +
|
|
|
|
- " local res = redis.pcall('HEXISTS', key, customerOrder)" +
|
|
|
|
- " if res == 1 then" +
|
|
|
|
- " return 0" +
|
|
|
|
- " else" +
|
|
|
|
- " redis.pcall('HSET', key, customerOrder, orderId)" +
|
|
|
|
- " return 1" +
|
|
|
|
- " end";
|
|
|
|
|
|
+ String slotKey = "{" + ORDER_DOWN.key() + ".0}";
|
|
|
|
+
|
|
if (Strings.isBlank(runCode) || !jedisCluster.scriptExists(runCode, slotKey)) {
|
|
if (Strings.isBlank(runCode) || !jedisCluster.scriptExists(runCode, slotKey)) {
|
|
|
|
+ log.info("脚本加载:" + slotKey);
|
|
//redis支持脚本缓存,返回哈希码,后续可以继续用来调用脚本
|
|
//redis支持脚本缓存,返回哈希码,后续可以继续用来调用脚本
|
|
runCode = jedisCluster.scriptLoad(SCRIPT, slotKey);
|
|
runCode = jedisCluster.scriptLoad(SCRIPT, slotKey);
|
|
} else {
|
|
} else {
|
|
log.info("脚本已加载");
|
|
log.info("脚本已加载");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
/*
|
|
@@ -53,8 +60,20 @@ public class RedisOrderIdScript {
|
|
* 返回 1 成功加入 0 下单号重复
|
|
* 返回 1 成功加入 0 下单号重复
|
|
*/
|
|
*/
|
|
public String checkOrderId(String customerOrder, String OrderId) {
|
|
public String checkOrderId(String customerOrder, String OrderId) {
|
|
- String orderName = ORDER_DOWN.key();
|
|
|
|
- Object res = jedisCluster.evalsha(runCode, Arrays.asList(orderName, orderName + customerOrder), Collections.singletonList(OrderId));
|
|
|
|
|
|
+ //计算key的hash
|
|
|
|
+ int value = customerOrder.hashCode() % 10000;
|
|
|
|
+ if (value < 0) {
|
|
|
|
+ value = Math.abs(value);
|
|
|
|
+ }
|
|
|
|
+ String orderName = "{" + ORDER_DOWN.key() + "." + value + "}";
|
|
|
|
+ Object res;
|
|
|
|
+ try {
|
|
|
|
+ res = jedisCluster.evalsha(runCode, Arrays.asList(orderName, orderName + customerOrder), Collections.singletonList(OrderId));
|
|
|
|
+ } catch (JedisNoScriptException e) {
|
|
|
|
+ jedisCluster.scriptLoad(SCRIPT, orderName);
|
|
|
|
+ res = jedisCluster.evalsha(runCode, Arrays.asList(orderName, orderName + customerOrder), Collections.singletonList(OrderId));
|
|
|
|
+ }
|
|
return res.toString();
|
|
return res.toString();
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|