lua脚本操作Redis

一次扣减一个商品库存

@Component

public class LuaDemo {

复制代码
@Autowired
private StringRedisTemplate redisTemplate;


public void test() {

  List<String> keys = new ArrayList<>();
    keys.add("yanjie007");

    String lua = "return redis.call('set',KEYS[1],ARGV[1])";

    RedisScript<String> redisScript=  RedisScript.of(lua,String.class);

    String shuai = stringRedisTemplate.execute(redisScript, keys, "今天真帅");

    System.out.println(shuai);

}

}

//扣减库存

@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) "); // 0, 1,2,3 ...

sb.append(" end ");

sb.append(" ");

RedisScript script = RedisScript.of(sb.toString(),Long.class);

ExecutorService executorService = Executors.newCachedThreadPool();

复制代码
    for (int i = 1; i <= 10; i++) {
        int finalI = i;
        executorService.execute(()->{
            int needQty = RandomUtil.randomInt(1, 5);
            Long qty =  stringRedisTemplate.execute(script, CollUtil.newArrayList(key), needQty+"");
         if(qty.intValue() == -1 ){
             System.out.println("线程"+ finalI +"扣减成功,需求量是:"+needQty);
          } else {

             System.out.println("线程"+ finalI +"扣减失败,当前库存变量是:"+qty);
         }
        });
    }


    System.in.read();
}

一次扣减多个商品的库存

void test4() {

复制代码
    StringBuilder sb = new StringBuilder();
    sb.append(" local table = {}  "); // 你要扣减的key :product.1
    sb.append(" local values =  redis.call('mget',  unpack(KEYS) )"); // [product.1,product.2]   =>  product.1 product.2
    sb.append(" for i = 1, #KEYS   do  ");
    sb.append("   if  tonumber(ARGV[i]) > tonumber(values[i])   then ");
    sb.append("     table[#table + 1] =  KEYS[i] .. '=' .. values[i] "); // product.1=23
    sb.append("   end ");
    sb.append(" end ");
    sb.append(" if #table > 0 then ");
    sb.append("   return table  ");
    sb.append(" end ");
    sb.append(" for i = 1 , #KEYS do ");
    sb.append("   redis.call('decrby',KEYS[i],ARGV[i])  ");
    sb.append(" end ");
    sb.append(" return {} ");
    RedisScript<List> luaScript = RedisScript.of(sb.toString(), List.class);

      List<StockProduct> stockProducts =  new ArrayList<>();
      stockProducts.add(new StockProduct(5,1));
      stockProducts.add(new StockProduct(4,2));

    List<String> keys = stockProducts.stream().map(it -> "product." + it.getId()).collect(Collectors.toList());
    Object[] qtys = stockProducts.stream().map(it -> it.getQty() + "").toArray();
    List<String> list = stringRedisTemplate.execute(luaScript,
            keys,
            qtys);
    if(list.isEmpty()){
        System.out.println("库存冻结成功");
    } else {

        for (String key_qty : list) {
            String[] split = key_qty.split("=");
            System.out.println(split[0] + "库存不足,剩余库存量:" + split[1]);
        }

    }


    ThreadUtil.safeSleep(3000);


}
相关推荐
morris1316 小时前
【redis】redis实现分布式锁
数据库·redis·缓存·分布式锁
爱的叹息8 小时前
spring boot集成reids的 RedisTemplate 序列化器详细对比(官方及非官方)
redis
weitinting8 小时前
Ali linux 通过yum安装redis
linux·redis
纪元A梦9 小时前
Redis最佳实践——首页推荐与商品列表缓存详解
数据库·redis·缓存
爱的叹息16 小时前
Java 连接 Redis 的驱动(Jedis、Lettuce、Redisson、Spring Data Redis)分类及对比
java·redis·spring
松韬17 小时前
Spring + Redisson:从 0 到 1 搭建高可用分布式缓存系统
java·redis·分布式·spring·缓存
天上掉下来个程小白17 小时前
Redis-14.在Java中操作Redis-Spring Data Redis使用方式-操作列表类型的数据
java·redis·spring·springboot·苍穹外卖
·云扬·18 小时前
深度剖析 MySQL 与 Redis 缓存一致性:理论、方案与实战
redis·mysql·缓存
汤姆大聪明18 小时前
Redisson 操作 Redis Stream 消息队列详解及实战案例
redis·spring·缓存·maven
csjane10791 天前
Redis原理:rename命令
java·redis