Note that there are some explanatory texts on larger screens.

plurals
  1. POGuava cache return an empty result on the second hit
    text
    copied!<p>I have a strange (at least for me) behaviour with guava cache. After the first hit, the following accesses will return an empty object. I did not use strange evinction, so I can't figure out where I'm doing wrong. I declared the following LoadingCache:</p> <pre><code>LoadingCache&lt;String, Vector&lt;Location&gt;&gt; locations = CacheBuilder.newBuilder() .maximumSize(100000) .build( new CacheLoader&lt;String,Vector&lt;Location&gt;&gt;() { @Override public Vector&lt;Location&gt; load(String key) { return _getLocationListByTranscriptId(key); } }); </code></pre> <p>and I used it only in this method:</p> <pre><code>public Vector&lt;Location&gt; getLocationListByTranscriptId (String transcriptid) { if (transcriptid.equals("TCONS_00000046")) System.out.println("tcons found, will this work?"); Vector&lt;Location&gt; result; try { result = locations.get(transcriptid); } catch (ExecutionException e) { System.err.println("Error accessing cache, doing the hard way"); result = _getLocationListByTranscriptId(transcriptid); } if (transcriptid.equals("TCONS_00000046")){ if (result.size()==0){ System.out.println("this is a problem"); return null; } System.out.println("this is good!"); } return result; } </code></pre> <p>Iterating a Collection of input string, i get the following output:</p> <pre><code>tcons found, will this work? this is good! tcons found, will this work? this is a problem </code></pre> <p>So, the first time i use the cache, it works, but A) the value is not correctly stored for future accesses; B) the value is resetted for some strange behaviour. What can i do? Thanks all for reading this!</p> <p>EDIT: Thanks to axtavt answer I could immediately figure out where I was editing the resulting list. Don't know why, i was sure about guava cache returning a copy of the values. Thank you for the answer, and for the suggestions about defensive programming. (Sorry if i can't rate your answer yet).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload