客户端增加扣减库存
This commit is contained in:
@@ -13,4 +13,5 @@ public class RedisCst {
|
||||
public static final String ONLINE_USER = "ONLINE:USER";
|
||||
public static final String CART = "CZG:CART:";
|
||||
|
||||
public static final Object PRODUCT = "PRODUCT:";
|
||||
}
|
||||
|
||||
@@ -577,4 +577,43 @@ public class RedisUtil {
|
||||
}
|
||||
return 0L ;
|
||||
}
|
||||
|
||||
String secKillScript = "local prodid=KEYS[1];\r\n" +
|
||||
"local usernum=KEYS[2];\r\n" +
|
||||
"local num= redis.call(\"get\" ,prodid);\r\n" +
|
||||
"if tonumber(num)<tonumber(usernum) then \r\n" +
|
||||
" return 0;\r\n" +
|
||||
"end\r\n" +
|
||||
"if tonumber(num)<=0 then \r\n" +
|
||||
" return 0;\r\n" +
|
||||
"else \r\n" +
|
||||
" redis.call(\"DECRBY\",prodid,tonumber(usernum));\r\n" +
|
||||
"end\r\n" +
|
||||
"return 1";
|
||||
public String seckill(String key,String num) {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
return REDIS_FAILED+"";
|
||||
}
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
Object result = jedis.eval(secKillScript, Arrays.asList( key,num), new ArrayList<>());
|
||||
String reString = String.valueOf(result);
|
||||
return reString;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return REDIS_FAILED+"";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user