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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| package com.ym.common.utils.redis;
import com.ym.common.utils.Sha1Util; import com.ym.common.utils.spring.SpringUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.script.RedisScript;
import java.util.Collections;
public class RedisLockUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisLockUtil.class);
private static final Long SUCCESS = 1L;
private static final String SCRIPT_LOCK = "if redis.call('setnx', KEYS[1], ARGV[1]) == 1 then redis.call('pexpire', KEYS[1], ARGV[2]) return 1 else return 0 end";
private static final String SCRIPT_UNLOCK = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
private static final String SCRIPT_LOCK_SHA1 = Sha1Util.encrypt(SCRIPT_LOCK);
private static final String SCRIPT_UNLOCK_SHA1 = Sha1Util.encrypt(SCRIPT_UNLOCK);
private static final RedisTemplate redisTemplate = (RedisTemplate) SpringUtil.getObject("redisTemplate");
public static boolean tryGetDistributedLock(String lockKey, String requestId, int expireTimeMilliseconds) { LOGGER.info("[{}]尝试获取[{}]锁,超时时间为:{}毫秒", requestId, lockKey, expireTimeMilliseconds);
RedisScript<Long> redisScript = new RedisScript<Long>() { @Override public String getSha1() { return SCRIPT_LOCK_SHA1; }
@Override public Class<Long> getResultType() { return Long.class; }
@Override public String getScriptAsString() { return SCRIPT_LOCK; }
}; Object result = redisTemplate.execute( redisScript, Collections.singletonList(lockKey), requestId, expireTimeMilliseconds ); boolean b = SUCCESS.equals(result); LOGGER.info("释放结果:", b); return b; }
public static boolean releaseDistributedLock(String lockKey, String requestId) { LOGGER.info("[{}]释放锁[{}]锁", requestId, lockKey);
RedisScript<Long> redisScript = new RedisScript<Long>() { @Override public String getSha1() { return SCRIPT_UNLOCK_SHA1; }
@Override public Class<Long> getResultType() { return Long.class; }
@Override public String getScriptAsString() { return SCRIPT_UNLOCK; }
}; Object result = redisTemplate.execute( redisScript, Collections.singletonList(lockKey), requestId ); boolean b = SUCCESS.equals(result); LOGGER.info("释放结果:", b); return b; } }
|