继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

通过redis限制每秒钟只能处理n个请求

judyW
关注TA
已关注
手记 47
粉丝 11
获赞 107

// 同一xx设置同时只可以操作8个并发的限制
if (xxx) {
String prefix = sipAccountInfo.getSipAccount();
String str = prefix.substring(0, prefix.indexOf("_"));
String today = MyDateUtils.formatLocalDateTime(LocalDateTime.now());
String concurrencyCalledCountKey = RedisKeyCenter.getConcurrencyCalledCountByGatewayKey(str, today);
Long result;
result = redisService.incrementKey(concurrencyCalledCountKey, 1);
result = result == null ? 0 : result;
if (result == 1) {
redisService.expire(concurrencyCalledCountKey, 1, TimeUnit.MINUTES);
}
while (result >= 8) {
logger.debug());
Thread.sleep(1000);
String newTime = MyDateUtils.formatLocalDateTime(LocalDateTime.now());
String countKey = RedisKeyCenter.getConcurrencyCalledCountByGatewayKey(str, newTime);
result = redisService.incrementKey(countKey, 1);
result = result == null ? 0 : result;
if (result == 1) {
redisService.expire(countKey, 1, TimeUnit.MINUTES);
}
}

        }
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP