package com.yiqi.lock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisLockUtil {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public boolean tryLock(String key) {
        return redisTemplate.opsForValue().setIfAbsent(key, "1", 30, TimeUnit.SECONDS);
    }

    public void unlock(String key) {
        redisTemplate.delete(key);
    }
}