接口的认证,访问量,频率的统计
来源:互联网 发布:mac上怎么卸载office 编辑:程序博客网 时间:2024/06/03 06:01
说明:应用中对接口的判断,调用次数和调用时间的限制问题
要求:
用户调用一方法,当第一次开始调用的时候开始计时,
在时间间隔 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测试:
@Testpublic 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();}}
0 0
- 接口的认证,访问量,频率的统计
- 网站访问量的统计
- 统计文章的访问量
- 极客头条访问量的统计
- 页面访问量和网站访问量的统计
- 统计网站接口访问量
- 统计网站当天的访问量
- 网站访问量的统计方式
- 统计某个端口的访问量
- 统计网站页面的访问量
- jsp统计页面访问量和刷访问量的简单使用
- 简单的javaBean实现访问量统计
- 静态HTML页面的访问量统计实现
- java实现页面访问量统计的实例
- mysql统计网站每天的访问量
- Servlet 实现访问量的统计小案例
- 使用ServletContext对象统计网站的访问量
- 统计网站访问量的小例子
- 批判矛盾本质论
- AngularJS消息
- ---------------------------------设计模式分割线----------------------------------------------------
- 解决WIN32窗口不响应WM_LBUTTONDBLCLK消息
- Servlet生命周期
- 接口的认证,访问量,频率的统计
- win7下建立软AP -- 方法一
- WPF数据绑定
- 一个一元二次方程求解编程引申的两个知识点(abs和fabs的区别以及浮点数比较相等)
- 让jquery validation plugin 支持zepto
- Ubuntu 13.04下构建Qt5开发环境
- 简单工厂模式
- java多线程简单小举例
- 巧用CUDA中的pinned memory