title: Guava Cache - maximumSize - Eviction (LRU or most often)
body: <p>I am planning to use Guava Cache in my application. Some of the <strong>[K, V]</strong> pairs that are accessed are statistically >> than others and should be available in the cache essentially always.</p>
  
  <p>According to the spec of <a href="https://code.google.com/p/guava-libraries/wiki/CachesExplained#Size-based_Eviction" rel="noreferrer">Sized Based Eviction</a> </p>
  
  <blockquote>
    <p>If your cache should not grow beyond a certain size, just use
    CacheBuilder.maximumSize(long). The cache will try to evict entries
    that haven't been used recently or very often</p>
  </blockquote>
  
  <p>I am trying to understand the <strong>very often</strong> part. Can I assume - for a cache of <code>maximumSize = 100</code>, if <code>Key_A</code> is accessed <code>10000</code> times and immediately post that there are <code>100</code> put calls, the <code>Key_A</code> would still be present in the cache? Basic unit test that I carried out doesn't seem to confirm this fact. <em>Any thoughts?</em></p>
  
  <pre><code>public class CacheHelper
  {
      private Cache&lt;String, String&gt; cache;
      public CacheHelper()
      {
          cache = CacheBuilder.newBuilder().maximumSize(10).concurrencyLevel(1).build();
      }
  
      public String fetchVal(String key)
      {
          String val = cache.getIfPresent(key);
          return val;
      }
  
      public void putVal(String key, String val)
      {
          cache.put(key, val);
      }
  }
  </code></pre>
  
  <p>Test</p>
  
  <pre><code>public class CacheTest
  {
      public static void main(String[] args)
      {
          CacheHelper cacheHelper = new CacheHelper();
          cacheHelper.putVal("0", "0");
          for(int i = 0; i &lt; 10000; i++)
          {
              cacheHelper.fetchVal("0");
          }
          System.out.println(cacheHelper.fetchVal("0"));
          for(int i = 1; i &lt; 11; i++)
          {
              String key = "" + i;
              cacheHelper.putVal(key, key);
          }
          System.out.println(cacheHelper.fetchVal("0"));  // Still returns NULL - "most often not being used?"
      }
  }
  </code></pre>
  
  <p>Thanks</p>