刘光辉
5 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package jnpf.flowable.service.dev;
 
import jnpf.flowable.util.RedisLock;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
/** Shares normal approval's node-lock namespace; token serialization must match the acquired value. */
@Component
@RequiredArgsConstructor
public class DevApprovalLocks {
    private final RedisLock nativeLocks;
    private final RedisTemplate<String, Object> redisTemplate;
 
    public boolean lock(String name, String token, long ttl, TimeUnit unit) {
        return nativeLocks.lock(name, token, ttl, unit);
    }
 
    public boolean unlock(String name, String token) {
        var script = new DefaultRedisScript<Long>(
                "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end", Long.class);
        // Pass the token itself as the vararg, not a List serialized as a different Redis value.
        Long result = redisTemplate.execute(script, List.of("workflow-lock:" + name), token);
        return result != null && result > 0;
    }
}