1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package cn.efunbox.base.util;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Component;
- /**
- * CommonRedisHelper
- * Created by wangys on 2019/04/09
- */
- /**
- * Description: 通用Redis帮助类
- * User: zhouzhou
- * Date: 2018-09-05
- * Time: 15:39
- */
- @Component
- public class CommonRedisHelper {
- @Autowired
- private RedisTemplate<String, String> redisTemplate;
- /**
- * 加锁
- *
- * @param key 键
- * @param value 当前时间 + 超时时间
- * @return 是否拿到锁
- */
- public boolean lock(String key, String value) {
- if (redisTemplate.opsForValue().setIfAbsent(key, value)) {
- return true;
- }
- String currentValue = redisTemplate.opsForValue().get(key);
- //如果锁过期
- if (!StringUtils.isEmpty(currentValue)
- && Long.parseLong(currentValue) < System.currentTimeMillis()) {
- String oldValue = redisTemplate.opsForValue().getAndSet(key, value);
- //是否已被别人抢占
- return StringUtils.isNotEmpty(oldValue) && oldValue.equals(currentValue);
- }
- return false;
- }
- /**
- * 解锁
- *
- * @param key 键
- * @param value 当前时间 + 超时时间
- */
- public void unlock(String key, String value) {
- try {
- String currentValue = redisTemplate.opsForValue().get(key);
- if (!StringUtils.isEmpty(currentValue) && currentValue.equals(value)) {
- redisTemplate.opsForValue().getOperations().delete(key);
- }
- } catch (Exception e) {
- // log.error("redis解锁异常");
- }
- }
- }
|