hgus1294 hgus1294 - 1 month ago 9
Java Question

Java collection with expiring entries

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

after reading this post but while entries (I only store FAIL-events) in the Cache appears to expire as expected, a call to
(to determine number of fails) includes also the expired entries. This appears to be how it is supposed to work according to the documentation, if I have not misunderstood things?? Is there any way to get the number of 'active' events from a Cache?

I guess an alternative solution is to use a CEP-framework like Esper but it seems like overkill and cumbersome for my simple needs. Does anyone have a completely different approach to suggest that would facilitate my requirement? Thanks


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.