google guava checking for item in cache

10,964

Solution 1

The javadoc of CacheLoader#load(String) states

Parameters:

  • key the non-null key whose value should be loaded

Returns:

  • the value associated with key; must not be null

Throws:

  • Exception - if unable to load the result

You've implemented it as returning null, that breaks the CacheLoader contract.

Solution 2

If you need manage null values in your Loader, use Guava Optional

@Override
public Optional<Image> load(String key) throws Exception {
    return Optional.fromNullable(getImage(key));
}

Image getImage(String key) throws ExecutionException {
    //your code to get image from database, or other source
    return yourCodeToGetImageFromTheSource(key);
}

Your client code, can be:

try {
    Optional<Image> imageCached = imageCache.get(fileName);
} catch (ExecutionException e1) {
    // TODO error handling
}

if (imageCached.isPresent()) {
    Image img = imageCached.get();
} else {
    //your code when img is null
}

Solution 3

First Of All you cannot return null from your load method. If you want check whether a certain key exists in your cache you can simply get the ConcurrentMap used within the LoadingCache through

Map<K,V> imageMap = imageCache.asMap()

And simply use that map as any other map i.e. use the containsKey method on the Map to check whether a key is present and so on

Share:
10,964
blu10
Author by

blu10

Updated on June 04, 2022

Comments

  • blu10
    blu10 almost 2 years

    I'm trying to use google guava cache on a program but am not quite getting how it works.

    I'm loading up the cache and then at a later stage i'm trying to check if an item exists in the cache, my code below doesnt quite work

    The getIfPresent returns null if it doesnt exist but the load which calls it bombs out after with the error

    Exception in thread "main" com.google.common.cache.CacheLoader$InvalidCacheLoadException: CacheLoader returned null for key

     private static LoadingCache<String, Image> imageCache
              = CacheBuilder.newBuilder()
                    .build(new CacheLoader<String, Image>() {
    
                @Override
                public Image load(String key) throws Exception {                    
                    if (getImage(key) != null) {                    
                        return getImage(key);                       
                    }               
                    return null;
                }                 
              });           
    
    public static Image getImage(String key) throws ExecutionException {
    
        return imageCache.getIfPresent(key);
    
    }
    

    this means i cant check for the presense of the item in the cache like so

        try {
            readImage = imageCache.get(fileName);
        } catch (ExecutionException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    
        if (readImage != null) {
    
    
        }
    

    can someone explain to me what i'm doing wrong here?

  • blu10
    blu10 almost 9 years
    thanks, so is there no way to achieve what im trying to do here with google guava?... im new to it and couldnt find that many examples
  • Sotirios Delimanolis
    Sotirios Delimanolis almost 9 years
    @blu10 I don't really understand what you are trying to do. Your cache loader is trying to use the cache to load something. The cache loader should provide the cache value for the key. You seem to be doing things backwards.
  • blu10
    blu10 almost 9 years
    i guess im not understanding how it works completely.... i was looking for a quick mechanism to cache objects and also be able to check it they were present in the cache... Checking that they are present is the part that im not getting....
  • Sotirios Delimanolis
    Sotirios Delimanolis almost 9 years
    @blu10 Don't register a CacheLoader. Just use getIfPresent. It will return the value if it is present or null if it's not.