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 8833145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:42:35+00:00 2026-06-14T08:42:35+00:00

I am using Guava to cache hot data. When the data does not exist

  • 0

I am using Guava to cache hot data. When the data does not exist in the cache, I have to get it from database:

public final static LoadingCache<ObjectId, User> UID2UCache = CacheBuilder.newBuilder()
        //.maximumSize(2000)
        .weakKeys()
        .weakValues()
        .expireAfterAccess(10, TimeUnit.MINUTES)
        .build(
        new CacheLoader<ObjectId, User>() {
            @Override
            public User load(ObjectId k) throws Exception {
                User u = DataLoader.datastore.find(User.class).field("_id").equal(k).get();
                return u;
            }
        });

My problem is when the data does not exists in database, I want it to return null and to not do any caching. But Guava saves null with the key in the cache and throws an exception when I get it:

com.google.common.cache.CacheLoader$InvalidCacheLoadException:
CacheLoader returned null for key shisoft.

How do we avoid caching null values?

  • 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-14T08:42:37+00:00Added an answer on June 14, 2026 at 8:42 am

    Just throw some Exception if user is not found and catch it in client code while using get(key) method.

    new CacheLoader<ObjectId, User>() {
        @Override
        public User load(ObjectId k) throws Exception {
            User u = DataLoader.datastore.find(User.class).field("_id").equal(k).get();
            if (u != null) {
                 return u;
            } else {
                 throw new UserNotFoundException();
            }
        }
    }
    

    From CacheLoader.load(K) Javadoc:

    Returns:  
      the value associated with key; must not be null  
    Throws:  
      Exception - if unable to load the result
    

    Answering your doubts about caching null values:

    Returns the value associated with key in this cache, first loading
    that value if necessary. No observable state associated with this
    cache is modified until loading completes
    .

    (from LoadingCache.get(K) Javadoc)

    If you throw an exception, load is not considered as complete, so no new value is cached.

    EDIT:

    Note that in Caffeine, which is sort of Guava cache 2.0 and “provides an in-memory cache using a Google Guava inspired API” you can return null from load method:

     Returns:
       the value associated with key or null if not found
    

    If you may consider migrating, your data loader could freely return when user is not found.

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

Sidebar

Related Questions

I am using the Google Guava CacheBuilder to create a Cache instance. My code
I'm using google guava 12 and have a map: Map<OccupancyType, BigDecimal> roomPrice; I have
I'm using MultiMap from Google Guava 12 like this: Multimap<Integer, OccupancyType> pkgPOP = HashMultimap.create();
I've got a Guava Cache (or rather, I am migrating from MapMaker to Cache
I was using com.google.common.collect.PrimitiveArrays from Google Collections, however I cannot find it in Guava,
Is there a sample application that is developed by using guava cache application and
I am trying to get a date range using Guava's new Range functionality, via
Using Guava, I would like to have a map that has the following properties:
I have been using guava for some time now and truly trusted it, until
I need some data structure that I can build from standard collections or using

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.