How to handle "null" value in CacheLoader.load(key) ?

11,717 views
Skip to first unread message

Sky

unread,
Jan 30, 2012, 3:21:43 AM1/30/12
to guava-discuss
hi, friends

I have a question about CacheLoader.load(key) method that what
should I do when I really can not find a value for the specific key,
for example, "no such record in database for the key".

Example code like this:

public class UserCacheLoader extends CacheLoader<String, User> {
public User load(String userId) throws Exception {
User user = userDao.find(userId);
if (user != null) {
return user;
} else {
//??
}
}
}

I know that in CacheLoader.load(key) method we should not return
a "null", the javadoc even told us "must not be null". And I had got a
"InvalidCacheLoadException" when I tried to return a null in test
case.

So, how to handle it in CacheLoader.load(key) in case the value is
not exist for the key ?

Any help will be great appreciated!

Best Regards
Sky Ao

Louis Wasserman

unread,
Feb 6, 2012, 4:33:47 PM2/6/12
to Sky, guava-discuss
You could use Optional...

But it might be more appropriate here to write your own wrapper class that works like Optional but has more appropriate method names.

Kevin Bourrillion

unread,
Feb 6, 2012, 5:25:34 PM2/6/12
to Louis Wasserman, Sky, guava-discuss
Hang on; the first question to ask is whether you expect the null result to be cached or not.  If you do, Louis is right; if you don't, throw an exception.
--
Kevin Bourrillion @ Google
Java Core Libraries Team
http://guava-libraries.googlecode.com

Ondrej Medek

unread,
Feb 9, 2012, 9:53:25 AM2/9/12
to guava-discuss
I suppose Sky Ao wants to cache null result, too.

BTW. Hibernate has a second level cache. Maybe better opition.
BTW2. Hibernate default 2nd level cache is a poor
HashtableCacheProvider. It would be great to have some well working
GuavaCacheProvider. EHCache is tricky to configure and IMHO overkill
for small projects.

On 6 ún, 23:25, Kevin Bourrillion <kev...@google.com> wrote:
> Hang on; the first question to ask is whether you expect the null result to
> be *cached* or not.  If you do, Louis is right; if you don't, throw an
> exception.
>
> On Mon, Feb 6, 2012 at 1:33 PM, Louis Wasserman
> <wasserman.lo...@gmail.com>wrote:
>
>
>
>
>
>
>
>
>
> > You could use Optional...
>
> > But it might be more appropriate here to write your own wrapper class that
> > works like Optional but has more appropriate method names.
>
> > Louis Wasserman
> > wasserman.lo...@gmail.com
> >http://profiles.google.com/wasserman.louis
> >> To get help:http://stackoverflow.com/questions/ask(use the tag "guava")
>
> >  --
> > guava-...@googlegroups.com
> > Project site:http://guava-libraries.googlecode.com
> > This group:http://groups.google.com/group/guava-discuss
>
> > This list is for general discussion.
> > To report an issue:http://code.google.com/p/guava-libraries/issues/entry
> > To get help:http://stackoverflow.com/questions/ask(use the tag "guava")

oltranz...@gmail.com

unread,
Jul 27, 2017, 6:17:18 AM7/27/17
to guava-discuss
Hello, It is really late to respond but someone new may come and get a help form this.
So, in my case I did

User user = cashe.getIfPresent(userId);//userId as a used cache key



so, you will get Null if the key exist or some value if the there is a corresponding object valueEnter code here...
Note: I am using Guava-18.0 jar

Sushrita Yerra

unread,
Feb 2, 2022, 5:12:44 AM2/2/22
to guava-discuss
Again, this is really late but just in case someone like me comes in looking, the getIfPresent seems to have a bug wherein a concurrent environment it returns null sometimes even if the value is present. So I think going with Kevin's ans would be suitable.
Reply all
Reply to author
Forward
0 new messages