package com.persagy.functions.weather.constant;

import java.util.HashMap;
import java.util.Map;

public class ConstBusinessStatic {
	private static Map<String, Integer> ip_api2num = new HashMap<>();
	/**
	 * ip访问接口增1
	 * @param ip ip地址
	 * @param api 业务接口名
	 */
	public static void add(String ip, String api) {
		synchronized (ip_api2num) {
			String key = ip+"_"+api;
			Integer oldNum = ip_api2num.get(key);
			if(oldNum == null) {
				oldNum = 1;
			}else {
				oldNum += 1;
			}
			ip_api2num.put(key, oldNum);
		}
	}
	
	/**
	 * 获取接口统计信息，并清空缓存
	 * @return {ip_api:访问次数}
	 */
	public static Map<String, Integer> getAndFlush() {
		Map<String, Integer> resultMap = new HashMap<>();
		synchronized (ip_api2num) {
			resultMap.putAll(ip_api2num);
			ip_api2num.clear();
		}
		return resultMap;
	}
}
