title: Simple Java String cache with expiration possibility
body: <p>I am looking for a concurrent Set with expiration functionality for a Java 1.5 application. It would be used as a simple way to store / cache names (i.e. String values) that expire after a certain time.</p>
  
  <p>The problem I'm trying to solve is that two threads should not be able to use the same name value within a certain time (so this is sort of a blacklist ensuring the same "name", which is something like a message reference, can't be reused by another thread until a certain time period has passed). I do not control name generation myself, so there's nothing I can do about the actual names / strings to enforce uniqueness, it should rather be seen as a throttling / limiting mechanism to prevent the same name to be used more than once per second.</p>
  
  <p>Example:
  Thread #1 does <code>cache.add("unique_string, 1)</code> which stores the name "unique_string" for 1 second.
  If any thread is looking for "unique_string" by doing e.g. <code>cache.get("unique_string")</code> within 1 second it will get a positive response (item exists), but after that the item should be expired and removed from the set.</p>
  
  <p>The container would at times handle 50-100 inserts / reads per second.</p>
  
  <p>I have really been looking around at different solutions but am not finding anything that I feel really suites my needs. It feels like an easy problem, but all solutions I find are way too complex or overkill.</p>
  
  <p>A simple idea would be to have a <code>ConcurrentHashMap</code> object with key set to "name" and value to the expiration time then a thread running every second and removing all elements whose value (expiration time) has passed, but I'm not sure how efficient that would be? Is there not a simpler solution I'm missing?</p>