Skip to content

Instantly share code, notes, and snippets.

@rzwitserloot
Created August 9, 2019 11:45
Show Gist options
  • Save rzwitserloot/93c43a92dd99878745a5aec29312bad8 to your computer and use it in GitHub Desktop.
Save rzwitserloot/93c43a92dd99878745a5aec29312bad8 to your computer and use it in GitHub Desktop.
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