1 2 3 4 5 6 7 8 9 10 11 12 13
| @Autowired private StringRedisTemplate rs;
public void redisFenBuShiLock() { ValueOperations<String, String> ops = rs.opsForValue(); String uuid = UUID.randomUUID().toString(); ops.setIfAbsent("lock", uuid, 20, TimeUnit.SECONDS); String scrapt = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; Long i = rs.execute(new DefaultRedisScript<>(scrapt, Long.class), Arrays.asList("lock"), uuid); }
|