Created
          August 9, 2019 11:45 
        
      - 
      
- 
        Save rzwitserloot/93c43a92dd99878745a5aec29312bad8 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | private static final Map<String, Long> allowed = new ConcurrentHashMap<String, Long>(); | |
| private static final AtomicLong lastCleanup = new AtomicLong(); | |
| private static final long MAX_DURATION = TimeUnit.MINUTES.toMillis(45); | |
| private static final long CLEANUP_INTERVAL = TimeUnit.MINUTES.toMillis(30); | |
| public static void process(String k) { | |
| long n = System.currentTimeMillis(); | |
| allowed.put(k, n); | |
| long m = lastCleanup.get(); | |
| if (n - m > CLEANUP_INTERVAL) { | |
| if (lastCleanup.compareAndSet(m, n)) { | |
| long mark = n - MAX_DURATION; | |
| for (String k : allowed.keySet()) { | |
| allowed.computeIfPresent(k, (key, value) -> (value == null || value.longValue() < mark) ? null : value); | |
| } | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment