admin
2021-07-06 abce02c7a61820f5d580f87364d542e817be429c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.tejia.lijin.app.util;
 
import java.util.HashMap;
import java.util.Map;
 
//重复事件响应过滤器
public class DuplicateEventFilterUtil {
    private static Map<String, Long> lastAllowTime = new HashMap<>();
 
    //是否允许事件响应
    public static synchronized boolean allowEvent(String key) {
        if (key == null)
            return true;
        if (lastAllowTime.get(key) == null)
            lastAllowTime.put(key, 0L);
        if (System.currentTimeMillis() - lastAllowTime.get(key) < 500)
            return false;
        lastAllowTime.put(key, System.currentTimeMillis());
        return true;
    }
 
    public static synchronized boolean allowEvent(String key,int ms) {
        if (key == null)
            return true;
        if (lastAllowTime.get(key) == null)
            lastAllowTime.put(key, 0L);
        if (System.currentTimeMillis() - lastAllowTime.get(key) < ms)
            return false;
        lastAllowTime.put(key, System.currentTimeMillis());
        return true;
    }
 
}