Fayaz
Fayaz

Reputation: 334

Map auto expire elements that are not accessed

Is there a implementation of java.util.Map that automatically expires its entries when not accessed for specified amount of time. I found many libraries that auto expires entry with some time limits. But I have extra condition. Entry should be removed only when it is not accessed for the time duration.

Upvotes: 0

Views: 709

Answers (2)

Amin J
Amin J

Reputation: 1209

You can use Guava's cache.

It has two options:

  1. expireAfterWrite: Expires the entry after a certain period of time since the the entry was added to the cache; or the most recent replacement of the value
  2. expireAfterAccess: Expires the entry after a certain period of time since the entry was last accessed in the cache.

What you want is number 2.

Upvotes: 3

qwelyt
qwelyt

Reputation: 776

If you want something that gets evicted after a specified amount of time, I would suggest Guavas Cache https://github.com/google/guava/wiki/CachesExplained#timed-eviction

If the entry isn't queried during the time specified, it gets auto-evicted.

Upvotes: 2

Related Questions