0

There is no answer to my question here!

For example, there is a ,

Map <K,V> map = new ConcurrentHashMap<>()

V - some object that can be in the Map for no more than 300 ms. When we add an element to the Map (or it could be a Queue data structure), a timer would be turned on, which would delete the element after 300 ms.

At the same time, if an element is in this primitive cache, then when accessing it, you need to update the timer for this object (that is, start counting from the beginning, for this requested object).

In fact, this is an LRU cache, it has a certain size, removes the least used element from it (I know how to do this part), but at the same time, each serviced element must have an independent timer that will remove such an element from this cache, and when such element (in that cache) updating, the timer for such an element must be reset to zero, and if no one requires the item during the specified time, the element is automatically deleted from the cache.

Who has any ideas how this could be organized?

skyho
  • 1,438
  • 2
  • 20
  • 47
  • 5
    Just a note, I recommend reducing the amount of bold you use. The more bold characters you use, the less it means and the harder it is to actually read your question. – Nexevis May 06 '22 at 19:55
  • @skyho Please show your attempts you have tried and the problems/error messages you get from your attempts. Also explain how the several solutions from the linked question does not solve your problem. – Progman May 06 '22 at 20:00
  • 1
    If I had to write it myself, I'd most probably write a class implementing `Map`, and managing an internal `Map>`. I'd most probably only delete entries when they are queried and timed-out, or when a new entry should be added, but capacity is reached. – Turing85 May 06 '22 at 20:18
  • 1
    Bluntly, this problem is much too hard to solve in a StackOverflow answer. Use an existing library like [Caffeine](https://github.com/ben-manes/caffeine). – Louis Wasserman May 06 '22 at 20:41
  • [Does this help in any way?](https://gist.github.com/pcan/16faf4e59942678377e0) – DevilsHnd - 退職した May 06 '22 at 20:53
  • Perhaps [`ExpiringMap`](https://github.com/jhalterman/expiringmap#expiration-policies). (I’ve not used it.) – Basil Bourque May 07 '22 at 05:45

0 Answers0