陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
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
30
package com.mzl.flower.service.payment;
 
import com.mzl.flower.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.concurrent.TimeUnit;
 
@Service
@Transactional
public class RedisLockService extends BaseService {
 
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
 
    public static final String LOCK_KEY_PAYMENT_NOTIFY_ = "LOCK_KEY_PAYMENT_NOTIFY_";
 
    public boolean getObjectLock(String key, String objectId){
        String kk = key + objectId;
        return redisTemplate.opsForValue().setIfAbsent(kk, "1", 30, TimeUnit.SECONDS);
    }
 
    public void releaseObjectLock(String key, String objectId){
        String kk = key + objectId;
        redisTemplate.delete(kk);
    }
 
}