I need to track certain events over a specified time frame and act if the number of events reaches a certain number. In more detail, I connect to an external service and submit requests which are acknowledged with a status that equals CONF or FAIL. I need to be able to monitor the responses to detect if I get an unusual number of fails in a given time frame, e.g. >3 fails during the last 5 seconds, so that I can check for errors and act accordingly. I could alternatively check for 3 fails in a row but I prefer a time based approach.
I have been testing Guava's
Getting the exact number of active elements from the
Cache would require locking the entire cache, which is extremely expensive. You might be able to use the
cleanUp() method to make sure that
size is not accidentally counting entries that have been quietly evicted, though.
I would not depend on this giving you exact results, but it should improve the accuracy of the results significantly.