| | |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | import org.apache.logging.log4j.core.util.UuidUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.BoundSetOperations; |
| | | import org.springframework.data.redis.core.HashOperations; |
| | |
| | | { |
| | | return redisTemplate.keys(pattern); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 加锁 |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public boolean lock(String key){ |
| | | Boolean hasKey = redisTemplate.hasKey(key); |
| | | if(!hasKey){ |
| | | redisTemplate.opsForValue().set(key, UuidUtil.getTimeBasedUuid().toString()); |
| | | return true; |
| | | } |
| | | //30秒的等待 |
| | | for (int i = 0; i < 60; i++) { |
| | | hasKey = redisTemplate.hasKey(key); |
| | | if(!hasKey){ |
| | | redisTemplate.opsForValue().set(key, UuidUtil.getTimeBasedUuid().toString()); |
| | | return true; |
| | | } |
| | | try { |
| | | Thread.sleep(500); |
| | | } catch (InterruptedException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 解锁 |
| | | * @param key |
| | | */ |
| | | public void unlock(String key){ |
| | | redisTemplate.delete(key); |
| | | } |
| | | } |