業(yè)務需求中經常有需要用到計數器的場景:譬如一個手機號一天限制發(fā)送5條短信、一個接口一分鐘限制多少請求、一個接口一天限制調用多少次等等。使用Redis的Incr自增命令可以輕松實現以上需求。以一個接口一天限制調用次數為例:
/**
* 是否拒絕服務
* @return
*/
private boolean denialOfService(String userId){
long count=JedisUtil.setIncr(DateUtil.getDate()+""+userId+""+"queryCarViolation", 86400);
if(count=10){
return false;
}
return true;
}
/**
* 查詢違章
* @param plateNumber車牌
* @param vin 車架號
* @param engineNo發(fā)動機
* @param request
* @param response
* @throws Exception
*/
@RequestMapping("/queryCarViolationList.json")
@AuthorizationApi
public void queryCarViolationList(@CurrentToken Token token,String plateNumber,String vin,
String engineNo,HttpServletRequest request,HttpServletResponse response) throws Exception {
String userId=token.getUserId();
//超過限制,攔截請求
if(denialOfService(userId)){
apiData(request, response, ReqJson.error(CarError.ONLY_5_TIMES_A_DAY_CAN_BE_FOUND));
return;
}
//沒超過限制,業(yè)務邏輯……
}
每次調用接口之前,先獲得下計數器自增后的值,如果小于限制,放行,執(zhí)行后面的代碼。如果大于限制,則攔截掉。
JedisUtil工具類:
public class JedisUtil {
protected final static Logger logger = Logger.getLogger(JedisUtil.class);
private static JedisPool jedisPool;
@Autowired(required = true)
public void setJedisPool(JedisPool jedisPool) {
JedisUtil.jedisPool = jedisPool;
}
/**
* 對某個鍵的值自增
* @author liboyi
* @param key 鍵
* @param cacheSeconds 超時時間,0為不超時
* @return
*/
public static long setIncr(String key, int cacheSeconds) {
long result = 0;
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
result =jedis.incr(key);
if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
logger.debug("set "+ key + " = " + result);
} catch (Exception e) {
logger.warn("set "+ key + " = " + result);
} finally {
jedisPool.returnResource(jedis);
}
return result;
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- 詳解利用redis + lua解決搶紅包高并發(fā)的問題
- Nginx+Lua+Redis構建高并發(fā)Web應用
- 如何利用Redis鎖解決高并發(fā)問題詳解
- Redis瞬時高并發(fā)秒殺方案總結
- PHP實現Redis單據鎖以及防止并發(fā)重復寫入
- jedispool連redis高并發(fā)卡死的問題
- 使用lua+redis解決發(fā)多張券的并發(fā)問題