I am using bucket4j to do some rate limiting in my project. I have 1M users Basically I have for each user 1 bucket I keep the buckets in a ConcurrentHashMap<String, Bucket> structure Not all users are connected at the same time and I need to remove entries in the map which have not been recently used I need to check periodically on the hashMap and delete those entries which bucket has not been consumed let's say in last 5 minutes
How can I do it ? I know I can have my own data structure but I was really expecting that Bucket could provide something last time a token was consumed
Is it possible ?
Bucket4j does not store last consumption time. I would recomend to avoid to manage collections of buckets by yourseld, because CaffeineProxyManager perfectly covers your case, see sources https://github.com/bucket4j/bucket4j/blob/8.1/bucket4j-caffeine/src/main/java/io/github/bucket4j/caffeine/CaffeineProxyManager.java
It uses Caffeine to manage collection of buckets and performs accourate bucket livetime calculation. Feel free to ask more clarifying questions in github disscussions https://github.com/bucket4j/bucket4j/discussions