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

说明:应用中对接口的判断,调用次数和调用时间的限制问题

要求:

用户调用一方法,当第一次开始调用的时候开始计时,
在时间间隔 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();
		}
	}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Spring AOP实现接口访问统计,可以按照以下步骤进行: 1. 定义一个注解@AccessCount,用于标记需要统计访问的方法。 ```java @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface AccessCount { } ``` 2. 定义一个切面类AccessCountAspect,用于统计被@AccessCount标记的方法的访问。这个类需要实现org.aspectj.lang.annotation.Aspect接口,并使用@Aspect注解标记。 ```java @Aspect @Component public class AccessCountAspect { private Map<String, Integer> accessCounts = new ConcurrentHashMap<>(); @Pointcut("@annotation(com.example.demo.annotation.AccessCount)") public void accessCountPointcut() {} @Around("accessCountPointcut()") public Object accessCountAround(ProceedingJoinPoint joinPoint) throws Throwable { String methodName = joinPoint.getSignature().toShortString(); int count = accessCounts.getOrDefault(methodName, 0); accessCounts.put(methodName, count + 1); return joinPoint.proceed(); } public Map<String, Integer> getAccessCounts() { return accessCounts; } } ``` 3. 在Spring配置文件中定义切面和切点。 ```xml <aop:aspectj-autoproxy/> <bean id="accessCountAspect" class="com.example.demo.aspect.AccessCountAspect"/> <aop:config> <aop:aspect ref="accessCountAspect"> <aop:pointcut id="accessCountPointcut" expression="execution(* com.example.demo.service..*(..))"/> <aop:around method="accessCountAround" pointcut-ref="accessCountPointcut"/> </aop:aspect> </aop:config> ``` 4. 在需要统计访问的方法上添加@AccessCount注解。 ```java @Service public class UserServiceImpl implements UserService { @AccessCount @Override public User getUserById(Long id) { // ... } } ``` 5. 在需要获取访问的地方,注入AccessCountAspect并调用getAccessCounts方法。 ```java @RestController @RequestMapping("/access-count") public class AccessCountController { @Autowired private AccessCountAspect accessCountAspect; @GetMapping("") public Map<String, Integer> getAccessCounts() { return accessCountAspect.getAccessCounts(); } } ``` 这样就可以使用Spring AOP实现接口访问统计了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值