title: In-memory caching objects in java
body: <p>I want to cache objects in memory. The requirements are as follows:</p>
  
  <ol>
  <li>Every record/object is associated with a unique key.</li>
  <li>400-500 records/objects to be stored. If the number of records increase beyond the specified limit, then the older records should be evicted.</li>
  <li>Records shouldn't be stored beyond 2 mins.</li>
  <li>Should scale down when JVM is running out of memory (kind of weak reference).</li>
  <li>Third party library cannot be used because its a small module and the intention is just to decrease the unnecessary network access.</li>
  <li>There are more writes, less reads</li>
  </ol>
  
  <p>Security is also a concern here because we are going to cache some sensitive data. This data will be cached in memory. Should I really worry about the security and encrypt the data?</p>
  
  <p>I am looking for a Java class which provides a similar functionality.</p>
  
  <p>Currently I am thinking of extending <code>WeakHashMap</code>, and implementing various private/public methods to adhere to the requirements. </p>
  
  <p>If you have any other idea please share here.</p>