set 명령을 설정했지만 만료 시간을 설정할 때 네트워크 지터 및 기타 이유로 설정에 실패하면 데드 카운터(유사)
III. 코드 재구성 、 1. Retis+Lua 잠금
package han.zhang.utils; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.script.DigestUtils; import org.springframework.data.redis.core.script.RedisScript; import java.util.Collections; import java.util.UUID; public class RedisLock { private static final LogUtils logger = LogUtils.getLogger(RedisLock.class); private final StringRedisTemplate stringRedisTemplate; private final String lockKey; private final String lockValue; private boolean locked = false; /** * 使用脚本在redis服务器执行这个逻辑可以在一定程度上保证此操作的原子性 * (即不会发生客户端在执行setNX和expire命令之间,发生崩溃或失去与服务器的连接导致expire没有得到执行,发生永久死锁) * <p> * 除非脚本在redis服务器执行时redis服务器发生崩溃,不过此种情况锁也会失效 */ private static final RedisScript<Boolean> SETNX_AND_EXPIRE_SCRIPT; static { StringBuilder sb = new StringBuilder(); sb.append("if (redis.call('setnx', KEYS[1], ARGV[1]) == 1) then\n"); sb.append("\tredis.call('expire', KEYS[1], tonumber(ARGV[2]))\n"); sb.append("\treturn true\n"); sb.append("else\n"); sb.append("\treturn false\n"); sb.append("end"); SETNX_AND_EXPIRE_SCRIPT = new RedisScriptImpl<>(sb.toString(), Boolean.class); } private static final RedisScript<Boolean> DEL_IF_GET_EQUALS; sb.append("if (redis.call('get', KEYS[1]) == ARGV[1]) then\n"); sb.append("\tredis.call('del', KEYS[1])\n"); DEL_IF_GET_EQUALS = new RedisScriptImpl<>(sb.toString(), Boolean.class); public RedisLock(StringRedisTemplate stringRedisTemplate, String lockKey) { this.stringRedisTemplate = stringRedisTemplate; this.lockKey = lockKey; this.lockValue = UUID.randomUUID().toString() + "." + System.currentTimeMillis(); private boolean doTryLock(int lockSeconds) { if (locked) { throw new IllegalStateException("already locked!"); } locked = stringRedisTemplate.execute(SETNX_AND_EXPIRE_SCRIPT, Collections.singletonList(lockKey), lockValue, String.valueOf(lockSeconds)); return locked; * 尝试获得锁,成功返回true,如果失败立即返回false * * @param lockSeconds 加锁的时间(秒),超过这个时间后锁会自动释放 public boolean tryLock(int lockSeconds) { try { return doTryLock(lockSeconds); } catch (Exception e) { logger.error("tryLock Error", e); return false; * 轮询的方式去获得锁,成功返回true,超过轮询次数或异常返回false * @param lockSeconds 加锁的时间(秒),超过这个时间后锁会自动释放 * @param tryIntervalMillis 轮询的时间间隔(毫秒) * @param maxTryCount 最大的轮询次数 public boolean tryLock(final int lockSeconds, final long tryIntervalMillis, final int maxTryCount) { int tryCount = 0; while (true) { if (++tryCount >= maxTryCount) { // 获取锁超时 return false; } try { if (doTryLock(lockSeconds)) { return true; } } catch (Exception e) { logger.error("tryLock Error", e); Thread.sleep(tryIntervalMillis); } catch (InterruptedException e) { logger.error("tryLock interrupted", e); * 解锁操作 public void unlock() { if (!locked) { throw new IllegalStateException("not locked yet!"); locked = false; // 忽略结果 stringRedisTemplate.execute(DEL_IF_GET_EQUALS, Collections.singletonList(lockKey), lockValue); private static class RedisScriptImpl<T> implements RedisScript<T> { private final String script; private final String sha1; private final Class<T> resultType; public RedisScriptImpl(String script, Class<T> resultType) { this.script = script; this.sha1 = DigestUtils.sha1DigestAsHex(script); this.resultType = resultType; @Override public String getSha1() { return sha1; public Class<T> getResultType() { return resultType; public String getScriptAsString() { return script; }
2, Redis+Lua 카운터 구현을 위한 참조 잠금
package han.zhang.utils; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.script.DigestUtils; import org.springframework.data.redis.core.script.RedisScript; import java.util.Collections; public class CountUtil { private static final LogUtils logger = LogUtils.getLogger(CountUtil.class); private final StringRedisTemplate stringRedisTemplate; /** * 使用脚本在redis服务器执行这个逻辑可以在一定程度上保证此操作的原子性 * (即不会发生客户端在执行setNX和expire命令之间,发生崩溃或失去与服务器的连接导致expire没有得到执行,发生永久死计数器) * <p> * 除非脚本在redis服务器执行时redis服务器发生崩溃,不过此种情况计数器也会失效 */ private static final RedisScript<Boolean> SET_AND_EXPIRE_SCRIPT; static { StringBuilder sb = new StringBuilder(); sb.append("local visitTimes = redis.call('incr', KEYS[1])\n"); sb.append("if (visitTimes == 1) then\n"); sb.append("\tredis.call('expire', KEYS[1], tonumber(ARGV[1]))\n"); sb.append("\treturn false\n"); sb.append("elseif(visitTimes > tonumber(ARGV[2])) then\n"); sb.append("\treturn true\n"); sb.append("else\n"); sb.append("end"); SET_AND_EXPIRE_SCRIPT = new RedisScriptImpl<>(sb.toString(), Boolean.class); } public CountUtil(StringRedisTemplate stringRedisTemplate) { this.stringRedisTemplate = stringRedisTemplate; public boolean isOverMaxVisitTimes(String key, int seconds, int maxTimes) throws Exception { try { return stringRedisTemplate.execute(SET_AND_EXPIRE_SCRIPT, Collections.singletonList(key), String.valueOf(seconds), String.valueOf(maxTimes)); } catch (Exception e) { logger.error("RedisBusiness>>>isOverMaxVisitTimes; get visit times Exception; key:" + key + "result:" + e.getMessage()); throw new Exception("already Over MaxVisitTimes"); } private static class RedisScriptImpl<T> implements RedisScript<T> { private final String script; private final String sha1; private final Class<T> resultType; public RedisScriptImpl(String script, Class<T> resultType) { this.script = script; this.sha1 = DigestUtils.sha1DigestAsHex(script); this.resultType = resultType; @Override public String getSha1() { return sha1; public Class<T> getResultType() { return resultType; public String getScriptAsString() { return script; }
(2) 테스트 코드 Rreeee 호출
(3) (3) 테스트 결과위 내용은 Redis+Lua 스크립트를 사용하여 카운터 인터페이스의 스와이프 방지 기능을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!