package cn.admin.srv.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Objects;
@Component
@Slf4j
public class CommonRedisHelper {
public static final String LOCK_PREFIX = "redis_lock";
public static final int LOCK_EXPIRE = 10 * 1000; // 锁过期时间ms
public static final int LOCK_EXPIRE_WAIT = 100; // 获取锁失败后的等待时间ms,等待时间过去后再次尝试获取
private static final int LOCK_TIMEOUT = 5 * 1000; //锁等待时间,ms
@Autowired
RedisTemplate redisTemplate;
public boolean lock(String key){
log.info("lock invoke, key = {}", key);
return (Boolean) redisTemplate.execute((RedisCallback) connection -> {
//获取锁的超时时间,超过这个时间还没获取到锁&#x
redis 分布式锁
于 2022-06-20 15:42:52 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)