欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot+Redis實(shí)現(xiàn)外呼頻次限制功能的項(xiàng)目實(shí)踐

 更新時(shí)間:2025年07月07日 09:03:11   作者:sean_it  
本文提出基于SpringBoot和Redis的外呼頻次限制解決方案,采用Redis計(jì)數(shù)器與滑動(dòng)窗口算法實(shí)現(xiàn)精準(zhǔn)控制,具有一定的參考價(jià)值,感興趣的可以了解一下

針對(duì)外呼場(chǎng)景中的號(hào)碼頻次限制需求(如每3天只能呼出1000通電話),我可以提供一個(gè)基于Spring Boot和Redis的完整解決方案。

方案設(shè)計(jì)

核心思路

  • 使用Redis的計(jì)數(shù)器+過(guò)期時(shí)間機(jī)制
  • 采用滑動(dòng)窗口算法實(shí)現(xiàn)精確控制
  • 通過(guò)Lua腳本保證原子性操作

實(shí)現(xiàn)步驟

1. 添加依賴(lài)

<!-- pom.xml -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

2. 配置Redis

# application.yml
spring:
  redis:
    host: localhost
    port: 6379
    password: 
    database: 0

3. 實(shí)現(xiàn)頻次限制服務(wù)

@Service
public class CallFrequencyService {
    
    private final StringRedisTemplate redisTemplate;
    
    private static final String CALL_COUNT_PREFIX = "call:count:";
    private static final String CALL_TIMESTAMP_PREFIX = "call:timestamp:";
    
    @Autowired
    public CallFrequencyService(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
    
    /**
     * 檢查并增加呼叫計(jì)數(shù)
     * @param callerNumber 主叫號(hào)碼
     * @param limit 限制次數(shù)
     * @param period 限制周期(秒)
     * @return 是否允許呼叫
     */
    public boolean checkAndIncrement(String callerNumber, int limit, long period) {
        String countKey = CALL_COUNT_PREFIX + callerNumber;
        String timestampKey = CALL_TIMESTAMP_PREFIX + callerNumber;
        
        // 使用Lua腳本保證原子性
        String luaScript = """
            local count = redis.call('get', KEYS[1])
            local timestamp = redis.call('get', KEYS[2])
            local now = tonumber(ARGV[3])
            
            if count and timestamp then
                if now - tonumber(timestamp) < tonumber(ARGV[2]) then
                    if tonumber(count) >= tonumber(ARGV[1]) then
                        return 0
                    else
                        redis.call('incr', KEYS[1])
                        return 1
                    end
                else
                    redis.call('set', KEYS[1], 1)
                    redis.call('set', KEYS[2], ARGV[3])
                    redis.call('expire', KEYS[1], ARGV[2])
                    redis.call('expire', KEYS[2], ARGV[2])
                    return 1
                end
            else
                redis.call('set', KEYS[1], 1)
                redis.call('set', KEYS[2], ARGV[3])
                redis.call('expire', KEYS[1], ARGV[2])
                redis.call('expire', KEYS[2], ARGV[2])
                return 1
            end
            """;
        
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptText(luaScript);
        redisScript.setResultType(Long.class);
        
        Long result = redisTemplate.execute(redisScript, 
            Arrays.asList(countKey, timestampKey),
            String.valueOf(limit), 
            String.valueOf(period),
            String.valueOf(System.currentTimeMillis() / 1000));
        
        return result != null && result == 1;
    }
    
    /**
     * 獲取剩余可呼叫次數(shù)
     * @param callerNumber 主叫號(hào)碼
     * @param limit 限制次數(shù)
     * @return 剩余次數(shù)
     */
    public int getRemainingCount(String callerNumber, int limit) {
        String countKey = CALL_COUNT_PREFIX + callerNumber;
        String countStr = redisTemplate.opsForValue().get(countKey);
        
        if (StringUtils.isBlank(countStr)) {
            return limit;
        }
        
        int used = Integer.parseInt(countStr);
        return Math.max(0, limit - used);
    }
}

4. 實(shí)現(xiàn)REST接口

@RestController
@RequestMapping("/api/call")
public class CallController {
    
    private static final int DEFAULT_LIMIT = 1000;
    private static final long DEFAULT_PERIOD = 3 * 24 * 60 * 60; // 3天(秒)
    
    @Autowired
    private CallFrequencyService callFrequencyService;
    
    @PostMapping("/check")
    public ResponseEntity<?> checkCallPermission(@RequestParam String callerNumber) {
        boolean allowed = callFrequencyService.checkAndIncrement(
            callerNumber, DEFAULT_LIMIT, DEFAULT_PERIOD);
        
        if (allowed) {
            return ResponseEntity.ok().body(Map.of(
                "allowed", true,
                "remaining", callFrequencyService.getRemainingCount(callerNumber, DEFAULT_LIMIT)
            ));
        } else {
            return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).body(Map.of(
                "allowed", false,
                "message", "呼叫次數(shù)超過(guò)限制"
            ));
        }
    }
    
    @GetMapping("/remaining")
    public ResponseEntity<?> getRemainingCount(@RequestParam String callerNumber) {
        int remaining = callFrequencyService.getRemainingCount(callerNumber, DEFAULT_LIMIT);
        return ResponseEntity.ok().body(Map.of(
            "remaining", remaining,
            "limit", DEFAULT_LIMIT
        ));
    }
}

5. 添加定時(shí)任務(wù)重置計(jì)數(shù)器(可選)

@Scheduled(cron = "0 0 0 * * ?") // 每天凌晨執(zhí)行
public void resetExpiredCounters() {
    // 可以定期清理過(guò)期的key,避免Redis積累太多無(wú)用key
    // 實(shí)際應(yīng)用中,依賴(lài)expire通常已經(jīng)足夠
}

方案優(yōu)化點(diǎn)

  • 分布式鎖:如果需要更精確的控制,可以在Lua腳本中加入分布式鎖
  • 多維度限制:可以擴(kuò)展為基于號(hào)碼+時(shí)間段的多維度限制
  • 熔斷機(jī)制:當(dāng)達(dá)到限制閾值時(shí),可以暫時(shí)熔斷該號(hào)碼的呼叫能力
  • 動(dòng)態(tài)配置:將限制參數(shù)配置在數(shù)據(jù)庫(kù)或配置中心,實(shí)現(xiàn)動(dòng)態(tài)調(diào)整

測(cè)試用例

@SpringBootTest
public class CallFrequencyServiceTest {
    
    @Autowired
    private CallFrequencyService callFrequencyService;
    
    @Test
    public void testCallFrequencyLimit() {
        String testNumber = "13800138000";
        int limit = 5;
        long period = 60; // 60秒
        
        // 前5次應(yīng)該成功
        for (int i = 0; i < limit; i++) {
            assertTrue(callFrequencyService.checkAndIncrement(testNumber, limit, period));
        }
        
        // 第6次應(yīng)該失敗
        assertFalse(callFrequencyService.checkAndIncrement(testNumber, limit, period));
        
        // 等待周期結(jié)束
        try {
            Thread.sleep(period * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        // 新周期應(yīng)該重新計(jì)數(shù)
        assertTrue(callFrequencyService.checkAndIncrement(testNumber, limit, period));
    }
}

這個(gè)方案能夠高效、準(zhǔn)確地實(shí)現(xiàn)外呼頻次限制功能,通過(guò)Redis的高性能和原子性操作保證系統(tǒng)的可靠性,適合在生產(chǎn)環(huán)境中使用。

備注:

1、什么時(shí)間來(lái)統(tǒng)計(jì)使用次數(shù),真正呼叫出去才應(yīng)該是使用了呼叫次數(shù),所以需要異步在話單里來(lái)進(jìn)行處理,且需要判斷話單的具體狀態(tài)是否認(rèn)為是這個(gè)號(hào)碼被使用了。

2、在獲取號(hào)碼階段只去判斷當(dāng)前的訪問(wèn)次數(shù)是否超過(guò)了限制頻次即可,這樣的壞處時(shí)并不能精準(zhǔn)的去控制頻率(會(huì)有一小部分的時(shí)差),需要在性能和精確度上做綜合的權(quán)衡。

到此這篇關(guān)于SpringBoot+Redis實(shí)現(xiàn)外呼頻次限制功能的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)SpringBoot+Redis外呼頻次限制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論