Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7838131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:59:24+00:00 2026-06-02T14:59:24+00:00

I have a strange (at least for me) behaviour with guava cache. After the

  • 0

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:

LoadingCache<String, Vector<Location>> locations = CacheBuilder.newBuilder()
            .maximumSize(100000)
            .build(
                    new CacheLoader<String,Vector<Location>>() {
                        @Override
                        public Vector<Location> load(String key)  {
                            return _getLocationListByTranscriptId(key);
                        }
                    });

and I used it only in this method:

public Vector<Location> getLocationListByTranscriptId (String transcriptid) {
    if (transcriptid.equals("TCONS_00000046"))  System.out.println("tcons found, will this work?");
    Vector<Location> 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;
}

Iterating a Collection of input string, i get the following output:

tcons found, will this work?
this is good!
tcons found, will this work?
this is a problem

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!

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:59:25+00:00Added an answer on June 2, 2026 at 2:59 pm

    I believe you accidentially clear the Vector somewhere in your code. There are two possibilities:

    • Vector is modified by the code that obtains it from the cache.

      This kind of mistakes can be prevented by making defensive copy (though it ruins the idea of caching), or returning immutable view of collection:

      LoadingCache<String, List<Location>> locations = CacheBuilder.newBuilder()
           .maximumSize(100000)
           .build(
                   new CacheLoader<String, List<Location>>() {
                       @Override
                       public List<Location> load(String key)  {
                           return Collections.unmodifiableList(
                               _getLocationListByTranscriptId(key));
                       }
                   }); 
      

      After changing the code this way it will be easy to spot the place where illegal modification of collection takes place.

      Note that there is no unmodifiable view of Vector, therefore List should be used instead.

    • _getLocationListByTranscriptId() stores its result in a field where it can be accessed by other methods (or other invocation of the same method). So, you should check that _getLocationListByTranscriptId() doesn’t leave any references to its result in fields.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have strange behaviour in Python/PyMongo. dbh = self.__connection__['test'] first = dbh['test_1'] second =
I cannot understand these 2 strange behaviour 1. First Behaviour I have declared a
I have a strange issue (at least for me :)) with the MySQL's locking
I have a really strange behaviour with my tablet interface (Android > 3). When
I have the following strange scenario with spring's transaction management: I have method A
Recently I have tackled a strange behaviour of .Net synchronous receive method. I needed
I found this strange behaviour in .NET and even after looking into CLR via
I have noticed a strange behaviour of FULL OUTER JOIN in Oracle 11. I
fellow coders, just came across a strange (at least to me) behaviour for the
I'm using hibernate 3.6.1 and i have a strange behaviour on the sql generated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.