|
@@ -1,27 +1,25 @@
|
|
|
package com.fire.admin.rest;
|
|
|
|
|
|
|
|
|
-import com.fire.es.OrderEsDto;
|
|
|
+
|
|
|
+import com.fire.common.redisscript.RedisAmountScript;
|
|
|
+import com.fire.common.redisscript.RedisOrderIdScript;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.logging.log4j.util.Strings;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
|
|
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
|
|
-import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
|
|
-import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import redis.clients.jedis.JedisCluster;
|
|
|
-
|
|
|
import javax.annotation.Resource;
|
|
|
import java.text.DateFormat;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.util.Arrays;
|
|
|
import java.util.Date;
|
|
|
|
|
|
+import static com.fire.dto.enums.RedisKey.ORDER_CUSTOMER;
|
|
|
+import static com.fire.dto.enums.RedisKey.ORDER_DOWN;
|
|
|
+
|
|
|
|
|
|
@Slf4j
|
|
|
@RestController
|
|
@@ -31,9 +29,16 @@ public class TestRest {
|
|
|
@Autowired
|
|
|
private ElasticsearchRestTemplate restTemplate;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private RedisAmountScript redisAmountScript;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private RedisOrderIdScript redisOrderIdScript;
|
|
|
+
|
|
|
@Resource
|
|
|
private JedisCluster jedisCluster;
|
|
|
|
|
|
+
|
|
|
private String runCode = "";
|
|
|
|
|
|
public Date timeSwap(String time) {
|
|
@@ -93,51 +98,12 @@ public class TestRest {
|
|
|
// restTemplate.index(indexQuery, indexCoordinates);
|
|
|
// return "sucees";
|
|
|
// }
|
|
|
-
|
|
|
-
|
|
|
@GetMapping("/redis")
|
|
|
public String redis() {
|
|
|
- //初始化 判断有无加载脚本 ·
|
|
|
- storeScript("{key}Amount");
|
|
|
- //传入四个参数分别为
|
|
|
- //key Hset key
|
|
|
- //id 客户ID
|
|
|
- //sub 减去 add加上
|
|
|
- //金额
|
|
|
- Object res = jedisCluster.evalsha(runCode,Arrays.asList("{key}Amount","{key}asd"),Arrays.asList("sub","111"));
|
|
|
- if (null != res) {
|
|
|
- log.info(res.toString());
|
|
|
- }
|
|
|
- return "sucees";
|
|
|
- }
|
|
|
-
|
|
|
- /*
|
|
|
- *脚本加载到redis
|
|
|
- */
|
|
|
- public void storeScript(String slotKey){
|
|
|
- //LUA脚本
|
|
|
- String SCRIPT = "local key = KEYS[1]" +
|
|
|
- " local id = KEYS[2]" +
|
|
|
- " local operating = ARGV[1]" +
|
|
|
- " local num = tonumber(ARGV[2])" +
|
|
|
- " local totle = nil" +
|
|
|
- " local res = redis.pcall('HGET', key, id)" +
|
|
|
- " if res then" +
|
|
|
- " if operating =='add' then" +
|
|
|
- " totle = tonumber(res) + num" +
|
|
|
- " redis.pcall('HSET', key, id, tostring(totle))" +
|
|
|
- " elseif operating=='sub' then" +
|
|
|
- " totle = tonumber(res) - num" +
|
|
|
- " redis.pcall('HSET', key, id, tostring(totle))" +
|
|
|
- " end" +
|
|
|
- " end" +
|
|
|
- " return totle";
|
|
|
- if (Strings.isBlank(runCode) || !jedisCluster.scriptExists(runCode, slotKey)){
|
|
|
- //redis支持脚本缓存,返回哈希码,后续可以继续用来调用脚本
|
|
|
- runCode = jedisCluster.scriptLoad(SCRIPT, slotKey);
|
|
|
- }else{
|
|
|
- log.info("脚本已加载");
|
|
|
- }
|
|
|
+ jedisCluster.hset(ORDER_CUSTOMER.key(),ORDER_CUSTOMER.key()+"123456","1000");
|
|
|
+ redisOrderIdScript.checkOrderId(ORDER_DOWN.key() + "asdfgh", "123456789");
|
|
|
+ String res = redisAmountScript.changeAmount(ORDER_CUSTOMER.key(),ORDER_CUSTOMER.key()+"123456","sub","500","1000");
|
|
|
+ return res;
|
|
|
}
|
|
|
|
|
|
|