接口的认证,访问量,频率的统计
2015-03-25 00:26
246 查看
说明:应用中对接口的判断,调用次数和调用时间的限制问题
要求:
用户调用一方法,当第一次开始调用的时候开始计时,
在时间间隔 len 内,time为第几个调用,
每次调用时 time 加1,最多访问次数 num。
len间隔内,调用次数time小于num时,返回 true
len间隔内,调用次数time大于num时,返回 false
len时间以后,再有调用的就重新计数。
表结构:
时间间隔和访问次数限制由数据库读出(如表)。
编写方法:
Junit4测试:
要求:
用户调用一方法,当第一次开始调用的时候开始计时,
在时间间隔 len 内,time为第几个调用,
每次调用时 time 加1,最多访问次数 num。
len间隔内,调用次数time小于num时,返回 true
len间隔内,调用次数time大于num时,返回 false
len时间以后,再有调用的就重新计数。
表结构:
时间间隔和访问次数限制由数据库读出(如表)。
编写方法:
private long lastExecuteTime=0;//上次更新时间 private long executeSep=0;//定义更新间隔时间,单位毫秒 private HashMap times=new HashMap<Cng_callcontrol,Long>();//第几次调用 public boolean authentication(String user_id,String url,String store_code) { try { String[] params={user_id,url,store_code}; times=new HashMap<Cng_callcontrol,Integer>(); String hql="from Cng_callcontrol e where e.user_id=? and e.store_url=? and e.store_code=?"; Session session=getSession(); Query query=session.createQuery(hql); query.setParameter(0, user_id); query.setParameter(1, url); query.setParameter(2, store_code); query.setCacheable(true); List<Cng_callcontrol> list=query.list(); if (list!=null&&list.size()>0) { Cng_callcontrol cng_callcontrol=list.get(0); String call_unit=cng_callcontrol.getCall_unit();//单位:时、分、秒 long call_timelen=cng_callcontrol.getCall_timelen();//调用时长 long call_num=cng_callcontrol.getCall_num();//调用次数 long unit=1000L;//一分钟等于1000毫秒 if ("M".equalsIgnoreCase(call_unit)) {//分 unit*=60L; }else if("H".equalsIgnoreCase(call_unit)){//时 unit*=3600L; } executeSep=call_timelen*unit; if (executeSep<=0) {//间隔小于零的时候返回false return false; } long now = System.currentTimeMillis(); //获取当前系统时间 if ((now - lastExecuteTime) > executeSep) { lastExecuteTime=now; times.put(cng_callcontrol, 1L); } System.out.println((Long)times.get(cng_callcontrol)); long value=(Long)times.get(cng_callcontrol)==null?0L:(Long)times.get(cng_callcontrol); if (value>call_num) { return false; } times.put(cng_callcontrol, value+1L); System.out.println("点击次数:"+value+",当前时间:"+now+",开启时间:"+lastExecuteTime+",间隔:"+(now-lastExecuteTime)+",仓库地址:"+url); return true; } } catch (Exception e) { e.printStackTrace(); } return false; }
Junit4测试:
@Test public void authentication() { final Cng_callcontrolDao cng_callcontrolDao = (Cng_callcontrolDaoImpl) dsContext .getBean("cng_callcontrolDao"); final String user_id = "ff8080814b45c70f014b45d36b770001"; final String url = "www.baidu.com"; final String store_code = "1"; final Cng_callcontrolDao cng_callcontrolDao1 = (Cng_callcontrolDaoImpl) dsContext .getBean("cng_callcontrolDao"); final String user_id1 = "ff8080814b45c70f014b45d36b770001"; final String url1 = "www.qq.com"; final String store_code1 = "2"; try { for (int i = 0; i < 6000; i++) { // Thread.sleep(100); System.out.print("\n循环:"+i+" "); cng_callcontrolDao.authentication(user_id, url, store_code); cng_callcontrolDao1.authentication(user_id1,url1, store_code1); } } catch (Exception e) { e.printStackTrace(); } }
相关文章推荐
- 指定时间段增量统计nginx日志不同接口的访问量
- 统计网站接口访问量
- shell脚本统计接口访问量
- 5. 报警系统QuickAlarm之频率统计及接口封装
- 利用VS2012对文本文件的单词频率统计,并分析结果
- 使用hadoop+中文分词统计小说里的用词频率
- php使用文本统计访问量的方法
- 百度站长统计访问量
- webservice接口访问频率的控制问题
- RESTful接口签名认证实现机制
- PHP统计数值数组中出现频率最多的10个数字的方法
- 基于数据库的在线人数,日访问量等统计
- 统计频率【map的用法】【字典序】
- 在线人数统计和总访问量统计
- 利用redis统计分布式集群中接口缓存命中情况
- 统计历年6级真题及答案中单词出现频率
- 如何在纯HTML的静态网页中添加一段统计网页访问量的JAVA Script代码?
- 字典树实现词频统计及频率字典序双重融合排序算法
- js实现精确统计网站访问量的代码分享
- 使用hash算法统计字符串中字符出现的频率