title: Using Guava for high performance thread-safe caching
body: <p>I am trying to implement a high performance thread-safe caching. Here is the code I have implemented. I don't want any on demand computing. Can I use cache.asMap() and retrieve the value safely? Even if the cache is set to have softValues?</p>
  
  <pre><code>  import java.io.IOException;
    import java.util.concurrent.ConcurrentMap;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.TimeoutException;
  
    import com.google.common.cache.Cache;
    import com.google.common.cache.CacheBuilder;
  
    public class MemoryCache {
  
      private static MemoryCache instance;
      private Cache&lt;String, Object&gt; cache;
  
      private MemoryCache(int concurrencyLevel, int expiration, int size) throws IOException {
  
          cache = CacheBuilder.newBuilder().concurrencyLevel(concurrencyLevel).maximumSize(size).softValues()
              .expireAfterWrite(expiration, TimeUnit.SECONDS).build();
      }
  
      static public synchronized MemoryCache getInstance() throws IOException {
          if (instance == null) {
                 instance = new MemoryCache(10000, 3600,1000000);
          }
          return instance;
      }
  
      public Object get(String key) {
          ConcurrentMap&lt;String,Object&gt; map =cache.asMap();
          return map.get(key);
      }
  
      public void put(String key, Object obj) {
          cache.put(key, obj);
      }
     }
  </code></pre>