Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Collection implementaiton with timeout of the elements

Is there some collection implementation supporting expiration of the elements.

For example:

Collection<User> cachedUsers = new ExpirableList<User>(10000);

where

public ExpirableList(final long timeout){...}

And after given time (10000ms in this particular example), added elements will be removed from the collection. By using this, we will prevent overflow of our cachedUsers collection.

like image 659
Kiril Kirilov Avatar asked Feb 20 '12 15:02

Kiril Kirilov


Video Answer


2 Answers

Yes, Guava supports a cache with timed expiration. See Guava Explained's page on caches.

An alternative is an LRU (least-recently used) cache that disposes of the oldest accessed element when a new element is inserted.

like image 100
Andy Thomas Avatar answered Sep 22 '22 00:09

Andy Thomas


It's not really clear how you're trying to use the collection, but Guava's CacheBuilder may help you.

like image 45
Jon Skeet Avatar answered Sep 21 '22 00:09

Jon Skeet