redis+lua实现分布式锁

2020-06-06  本文已影响0人  南柯一梦00
 RedisScript redisScript =
                RedisScript.of("local tmp = redis.call('get', KEYS[1])\n" +
                                "if tmp == false then\n" +
                                "   redis.call('set',KEYS[1],KEYS[2])\n" +
                                "   return 1\n" +
                                "end\n" +
                                "return 0\n",
                        Long.class);
        List<String> list = new ArrayList<>();
        list.add("test-lock");
        list.add("test-lock");
        Long execute = (Long) redisTemplate.execute(redisScript, list, "60");
        System.err.println("执行结果:" + execute);
上一篇下一篇

猜你喜欢

热点阅读