jlzzjlzz亚洲乱熟在线播放

系統城裝機大師 - 唯一官網:www.farandoo.com!

當前位置:首頁 > 數據庫 > Redis > 詳細頁面

Redis+Lua腳本實現計數器接口防刷功能(升級版)

時間:2022-02-09來源:www.farandoo.com作者:電腦系統城

【前言】

Cash Loan(一):Redis實現計數器防刷 中介紹了項目中應用redis來做計數器的實現過程,最近自己看了些關于Redis實現分布式鎖的代碼后,發現在Redis分布式鎖中出現一個問題在這版計數器中同樣會出現,于是融入了Lua腳本進行升級改造有了Redis+Lua版本。

【實現過程】

一、問題分析

 如果set命令設置上,但是在設置失效時間時由于網絡抖動等原因導致沒有設置成功,這時就會出現死計數器(類似死鎖);

二、解決方案

 Redis+Lua是一個很好的解決方案,使用腳本使得set命令和expire命令一同達到Redis被執行且不會被干擾,在很大程度上保證了原子操作;

為什么說是很大程度上保證原子操作而不是完全保證?因為在Redis內部執行的時候出問題也有可能出現問題不過概率非常??;即使針對小概率事件也有相應的解決方案,比如解決死鎖一個思路值得參考:防止死鎖會將鎖的值存成一個時間戳,即使發生沒有將失效時間設置上在判斷是否上鎖時可以加上看看其中值距現在是否超過一個設定的時間,如果超過則將其刪除重新設置鎖。       

三、代碼改造

1、Redis+Lua鎖的實現

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
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計數器

(1)工具類            

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
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)調用測試代碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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("超過i:" + i + ":" + overMax);
               } else {
                   System.out.println("沒超過i:" + i + ":" + overMax);
               }
           }
       } catch (Exception e) {
           logger.error("Exception {}", e.getMessage());
       }
   }

(3)測試結果

【總結】

       1、用心去不斷的改造自己的程序;

       2、用代碼改變世界。

到此這篇關于Redis+Lua實現計數器接口防刷(升級版)的文章就介紹到這了

分享到:

相關信息

系統教程欄目

欄目熱門教程

人氣教程排行

站長推薦

熱門系統下載