接口的认证,访问量,频率的统计

来源:互联网 发布: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
原创粉丝点击