接口调用频繁限制,接口限制流量
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                接口调用频繁限制,接口限制流量
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                這是第一種接口的方式,可能對性能有些影響,但是同一數(shù)據(jù)只能保存一次,感覺有點(diǎn)麻煩,過期數(shù)據(jù)無法清理,緩存會(huì)越變越大,要考慮數(shù)據(jù)如何重復(fù)和數(shù)據(jù)清除問題,沒有redis方便
并發(fā)安全的KEY static ConcurrentHashMap<String, Long> overdues = new ConcurrentHashMap<String, Long>();public R list(@RequestParam Map<String, Object> params) {Map<String, Object> map = new HashMap<>();try {boolean ispass=false; //標(biāo)記暫時(shí)未用到long t1 = System.currentTimeMillis();long t1 = System.currentTimeMillis();// 上一次調(diào)用3秒內(nèi)不調(diào)用if ( overdues.containsKey(json.toString())) {long pass = t1 - overdues.get(json.toString());if (pass >= 2000) {overdues.remove(json.toString());overdues.put(json.toString(), System.currentTimeMillis());} else {return R.error("保存太頻繁,請稍后再試!");}}else {overdues.put(json.toString(), System.currentTimeMillis());} //正常返回?cái)?shù)據(jù)后,應(yīng)該刪除緩存里的過期時(shí)間 String urlparam = url+"/order/save";JSONObject orderShow = Optional.ofNullable(postData(jsonObject,urlparam)).orElse(null);if (orderShow.getIntValue("code") == 0){overdues.remove(json.toString());//刪除定時(shí)緩存return R.ok("操作成功");}另一種就是redis?保證過期時(shí)間來防止重復(fù)提交,只是改用第三方中間件實(shí)現(xiàn),3秒過期,如果能返回?cái)?shù)據(jù)就是沒有過期,
也是很方便,暫時(shí)沒有過濾其它方面的影響
?
ValueOperations<String, String> properation = redisTemplate.opsForValue();String str = properation.get(params.toString());if (StringUtils.isBlank(str)) {properation.set(params.toString(), params.toString(), 3, TimeUnit.SECONDS);}else{return R.ok("調(diào)用太頻繁,請休息一下");}?
總結(jié)
以上是生活随笔為你收集整理的接口调用频繁限制,接口限制流量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 显卡的指标有哪些方面_纯干货!显卡购买重
- 下一篇: 五月券商金工精选
