The realization of the statistics of the user traffic of the website

1. Project requirements: An IP visit within one day is regarded as a visit, and the user visits of the website are counted.

2. Implementation idea: when the server starts, cache the traffic in the database, refresh the traffic when the user accesses, store the access information in the collection for caching, and store the access information in the database when it reaches a certain number. When it is closed, the access volume that is not stored in the database is stored in the database

3. Code implementation:

    //Used to count accesses corresponding to different URLs
    private static List<Map<String, Integer>> accessNumMapList = new ArrayList<Map<String, Integer>>();
    //Total access
    public static int accessSum = 0;
    //Used to cache the access ip of the day, the vector is synchronized, thread-safe
    public static Vector<String> ipVector = new Vector<String>();
    //The information used to cache the access volume
    private static List<AccessStatistics> asList = new ArrayList<AccessStatistics>();
    
    //Timed task, in the early morning of every day, the number of visits of the day is returned to 0
    public void accessTodayJob() {
        ipVector = new Vector<String>();
    }
    
    //This is called when the server starts Method, using the way of annotation development, to cache the corresponding number of URL classification, the total number, the number of the day
    @PostConstruct
    public void getAccessNum() {
        accessNumMapList = dao.getAccessUrlNum ();
        accessSum = dao.getAccessSum();
        ipVector = dao.getIpListToday();
    }    
    
    //Process the request and refresh the data
    public synchronized void accessMethod(HttpServletRequest request) {
        boolean flag = false;
        for (String str : ipVector) {
            if(request .getRemoteAddr().equals(str)) {
                flag = true;
                break;
            }
        }
        if(!flag) {
            ipVector.addElement(request.getRemoteAddr());
            accessSum ++;
            
            boolean containUrl = false;
            //Indicates that url: number
            for (Map<String, Integer> accessNumMap : accessNumMapList) {
                //如果存在该url,进行count++
                if(accessNumMap.containsKey(request.getRequestURI())) {
                    int count = accessNumMap.get(request.getRequestURI());
                    count = count + 1;
                    accessNumMap.put(request.getRequestURI(), count);
                    containUrl = true;
                    //跳出循环
                    break;
                }
            }
            
            if(!containUrl) {
                Map<String, Integer> accessNumMap = new HashMap<String, Integer>();
                accessNumMap.put(request.getRequestURI(), 1);
                accessNumMapList.add(accessNumMap);
            }
                    
            AccessStatistics as = new AccessStatistics();
            as.setRemoteAddr(request.getRemoteAddr());
            as.setRequestUrl(request.getRequestURI());
            as.setMethod(request.getMethod());
            as.setRequestTime(new Date()) ;
            asList.add(as);
            //Configure the size of the cache in the configuration file here, just restart the server without compiling
            if(asList.size() >= Integer.valueOf(Global.getConfig("accessCacheSize"))) {
                new SaveThread().start();
            }
        }
    }
    
    //Open a thread to process the save operation to improve performance
    private class SaveThread extends Thread{
        private boolean flag = false;//The thread security is guaranteed by using identifiers here , and improve performance.
        // If you open this thread next time you enter the method, go directly to the run method
        public void run() {
            if(flag) {
                return;
            }
            flag = true;
            for (AccessStatistics accessStatistics : asList) {
                save(accessStatistics);
            }
            //clear memory
            asList = new ArrayList<AccessStatistics>();
            flag = false;
        }
    }
    
    //Close the server and store the data not stored in the database into the database
    @PreDestroy
    public void serverStop() {
        if(asList.size() > 0) {
            new SaveThread().start();
        }
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325912235&siteId=291194637