3

Is there a way in Java to create a List implementation that has a timeout/eviction policy for items contained in the list? Something along the lines of:

List<SomeObject> myList = new TimeoutList<>(10, TimeUnit.SECONDS);

so that items entered into the list will be automatically evicted after the given time. I know about the Guava Cache, but a cache is a Map and will not maintain the order of the items entered.

Jewels
  • 966
  • 11
  • 28
  • 1
    Does this answer your question? [Size-limited queue that holds last N elements in Java](https://stackoverflow.com/questions/5498865/size-limited-queue-that-holds-last-n-elements-in-java) – PythonLearner Jan 05 '20 at 15:18
  • what happens if you access an index which is no longer contained? Is the item at index 1 a different one after five minutes because the original one was evicted and the item at index 2 went on to become index 1? – luk2302 Jan 05 '20 at 15:18
  • what happens if you iterate over the list and during the time of iteration you reach the expiration time? – luk2302 Jan 05 '20 at 15:19
  • From the look the question he is not bothered about this. He wants to maintain order if it's not evicted. – Arun Gupta Jan 05 '20 at 15:26
  • 1
    How about maintaining a `LinkedHashSet` of the cache keys alongside Guava Cache? Add a removal listener that removes from the linked hash map whenever an eviction happens. Make sure you put the key in the linked hash set whenever you add something to the cache. – RealSkeptic Jan 05 '20 at 15:36
  • @RealSkeptic OR maintain your own LinkedHashSet whith a class implementing Runnable and sleeping for the given TimeUnit before removing the element. I mean the Runnable would sleep and then finally remove the element. One such Runnable per element/value. – gthanop Jan 05 '20 at 15:43
  • This sounds like a job for [DelayQueue](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/DelayQueue.html), accompanied by a single background thread consisting of a `while (true) delayQueue.take();` loop. – VGR Jan 05 '20 at 16:43
  • @gthanop In principle, avoid creating runnables that sleep. There are schedulers and timers for this. In any case, the point is that the actual objects being cached may not be unique, so it would be better to use a map with a unique key. – RealSkeptic Jan 05 '20 at 16:59
  • @RealSkeptic can you give me some examples of scheduler(s)? You mean like [java.util.Timer](https://docs.oracle.com/javase/8/docs/api/java/util/Timer.html) and [javax.swing.Timer](https://docs.oracle.com/javase/8/docs/api/javax/swing/Timer.html) or is there something that I miss? The actual objects being cached may not be unique, but their Runnables would be (I wrote *One such Runnable per element/value*). – gthanop Jan 05 '20 at 17:10
  • @gthanop [`Executors.newScheduledThreadPool(…)`](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newScheduledThreadPool-int-) – Holger Jan 06 '20 at 09:22
  • @Holger great, thank you. – gthanop Jan 06 '20 at 09:58

0 Answers0