Redis+Lua腳本實(shí)現(xiàn)計(jì)數(shù)器接口防刷功能(升級(jí)版)
【前言】
Cash Loan(一):Redis實(shí)現(xiàn)計(jì)數(shù)器防刷 中介紹了項(xiàng)目中應(yīng)用redis來(lái)做計(jì)數(shù)器的實(shí)現(xiàn)過(guò)程,最近自己看了些關(guān)于Redis實(shí)現(xiàn)分布式鎖的代碼后,發(fā)現(xiàn)在Redis分布式鎖中出現(xiàn)一個(gè)問(wèn)題在這版計(jì)數(shù)器中同樣會(huì)出現(xiàn),于是融入了Lua腳本進(jìn)行升級(jí)改造有了Redis+Lua版本。
【實(shí)現(xiàn)過(guò)程】
一、問(wèn)題分析
如果set命令設(shè)置上,但是在設(shè)置失效時(shí)間時(shí)由于網(wǎng)絡(luò)抖動(dòng)等原因?qū)е聸](méi)有設(shè)置成功,這時(shí)就會(huì)出現(xiàn)死計(jì)數(shù)器(類(lèi)似死鎖);
二、解決方案
Redis+Lua是一個(gè)很好的解決方案,使用腳本使得set命令和expire命令一同達(dá)到Redis被執(zhí)行且不會(huì)被干擾,在很大程度上保證了原子操作;
為什么說(shuō)是很大程度上保證原子操作而不是完全保證?因?yàn)樵赗edis內(nèi)部執(zhí)行的時(shí)候出問(wèn)題也有可能出現(xiàn)問(wèn)題不過(guò)概率非常??;即使針對(duì)小概率事件也有相應(yīng)的解決方案,比如解決死鎖一個(gè)思路值得參考:防止死鎖會(huì)將鎖的值存成一個(gè)時(shí)間戳,即使發(fā)生沒(méi)有將失效時(shí)間設(shè)置上在判斷是否上鎖時(shí)可以加上看看其中值距現(xiàn)在是否超過(guò)一個(gè)設(shè)定的時(shí)間,如果超過(guò)則將其刪除重新設(shè)置鎖。
三、代碼改造
1、Redis+Lua鎖的實(shí)現(xiàn)
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服務(wù)器執(zhí)行這個(gè)邏輯可以在一定程度上保證此操作的原子性 * (即不會(huì)發(fā)生客戶(hù)端在執(zhí)行setNX和expire命令之間,發(fā)生崩潰或失去與服務(wù)器的連接導(dǎo)致expire沒(méi)有得到執(zhí)行,發(fā)生永久死鎖) * <p> * 除非腳本在redis服務(wù)器執(zhí)行時(shí)redis服務(wù)器發(fā)生崩潰,不過(guò)此種情況鎖也會(huì)失效 */ 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 加鎖的時(shí)間(秒),超過(guò)這個(gè)時(shí)間后鎖會(huì)自動(dòng)釋放 public boolean tryLock(int lockSeconds) { try { return doTryLock(lockSeconds); } catch (Exception e) { logger.error("tryLock Error", e); return false; * 輪詢(xún)的方式去獲得鎖,成功返回true,超過(guò)輪詢(xún)次數(shù)或異常返回false * @param lockSeconds 加鎖的時(shí)間(秒),超過(guò)這個(gè)時(shí)間后鎖會(huì)自動(dòng)釋放 * @param tryIntervalMillis 輪詢(xún)的時(shí)間間隔(毫秒) * @param maxTryCount 最大的輪詢(xún)次數(shù) public boolean tryLock(final int lockSeconds, final long tryIntervalMillis, final int maxTryCount) { int tryCount = 0; while (true) { if (++tryCount >= maxTryCount) { // 獲取鎖超時(shí) 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; // 忽略結(jié)果 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、借鑒鎖實(shí)現(xiàn)Redis+Lua計(jì)數(shù)器
(1)工具類(lèi)
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服務(wù)器執(zhí)行這個(gè)邏輯可以在一定程度上保證此操作的原子性 * (即不會(huì)發(fā)生客戶(hù)端在執(zhí)行setNX和expire命令之間,發(fā)生崩潰或失去與服務(wù)器的連接導(dǎo)致expire沒(méi)有得到執(zhí)行,發(fā)生永久死計(jì)數(shù)器) * <p> * 除非腳本在redis服務(wù)器執(zhí)行時(shí)redis服務(wù)器發(fā)生崩潰,不過(guò)此種情況計(jì)數(shù)器也會(huì)失效 */ 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)調(diào)用測(cè)試代碼
public void run(String... strings) { CountUtil countUtil = new CountUtil(SpringUtils.getStringRedisTemplate()); try { for (int i = 0; i < 10; i++) { boolean overMax = countUtil.isOverMaxVisitTimes("zhanghantest", 600, 2); if (overMax) { System.out.println("超過(guò)i:" + i + ":" + overMax); } else { System.out.println("沒(méi)超過(guò)i:" + i + ":" + overMax); } } } catch (Exception e) { logger.error("Exception {}", e.getMessage()); } }
(3)測(cè)試結(jié)果
【總結(jié)】
1、用心去不斷的改造自己的程序;
2、用代碼改變世界。
到此這篇關(guān)于Redis+Lua實(shí)現(xiàn)計(jì)數(shù)器接口防刷(升級(jí)版)的文章就介紹到這了,更多相關(guān)Redis計(jì)數(shù)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis如何使用zset處理排行榜和計(jì)數(shù)問(wèn)題
Redis的ZSET數(shù)據(jù)結(jié)構(gòu)非常適合處理排行榜和計(jì)數(shù)問(wèn)題,它可以在高并發(fā)的點(diǎn)贊業(yè)務(wù)中高效地管理點(diǎn)贊的排名,并且由于ZSET的排序特性,可以輕松實(shí)現(xiàn)根據(jù)點(diǎn)贊數(shù)實(shí)時(shí)排序的功能2025-02-02redis.clients.jedis.exceptions.JedisBusyException無(wú)法處理異常的解決方法
redis.clients.jedis.exceptions.JedisBusyException異常通常不是 Jedis客戶(hù)端直接拋出的標(biāo)準(zhǔn)異常,本文就來(lái)介紹一下異常的解決方法,感興趣的可以了解一下2024-05-05redis-cli創(chuàng)建redis集群的實(shí)現(xiàn)
本文主要介紹了redis-cli創(chuàng)建redis集群的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06Satoken+Redis實(shí)現(xiàn)短信登錄、注冊(cè)、鑒權(quán)功能
這篇文章主要介紹了Satoken+Redis實(shí)現(xiàn)短信登錄、注冊(cè)、鑒權(quán)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01淺談redis的maxmemory設(shè)置以及淘汰策略
下面小編就為大家?guī)?lái)一篇淺談redis的maxmemory設(shè)置以及淘汰策略。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03Redis集群水平擴(kuò)展、集群中添加以及刪除節(jié)點(diǎn)的操作
這篇文章主要介紹了Redis集群水平擴(kuò)展、集群中添加以及刪除節(jié)點(diǎn)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03Redis特殊數(shù)據(jù)類(lèi)型Geospatial地理空間
這篇文章主要為大家介紹了Redis特殊數(shù)據(jù)類(lèi)型Geospatial地理空間,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Redis中ziplist壓縮列表的實(shí)現(xiàn)
本文主要介紹了Redis中ziplist壓縮列表的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06