lua脚本操作Redis
一次扣减一个商品库存
**
@Test
void test2() throws IOException {
valueOperations.set(key,15L);
StringBuilder sb = new StringBuilder();
sb.append(" local key = KEYS[1] ");
sb.append(" local qty = ARGV[1] ");
sb.append(" local redis_qty = redis.call('get',key) ");
sb.append(" if tonumber(redis_qty) >= tonumber(qty) then ");
sb.append(" redis.call('decrby',key,qty) ");
sb.append(" return -1 ");
sb.append(" else ");
sb.append(" return tonumber(redis_qty) ");
sb.append(" end ");
sb.append(" ");
RedisScript<Long> script = RedisScript.of(sb