1

I am using guava cache in my application to boost performance. I have inserted 850 entries in the caches using putAll API in LoadingCache . My configured max cache size is 20K entries. I am testing cache size using test class TestCacheSize:

public class MetadataCacheKey{
    // implementation
}

public class Metadata{
    // implementation
}

public class MetadataCacheLoader extends CacheLoader<MetadataCacheKey, Optional<Metadata>> {
    /**
     * Guava cache does not support null values in cache. So null response from metadata source is wrapped in Optional
     * and loaded in cache. This reduces look ups for keys which are not present in source.
     */
    @Override
    public Optional<Metadata> load(@NonNull MetadataCacheKey key) throws Exception {
        return Optional.fromNullable(loadMetadataFromSource(key));
    }
    private Metadata loadMetadataFromSource(MetadataCacheKey key) {
        // implementation
    }
}

public class TestCacheSize {
    public static void main(String[] args) {
        LoadingCache<MetadataCacheKey, Optional<Metadata>> metadataCache = CacheBuilder.from(
                "expireAfterWrite=4h,maximumSize=20000").build(new MetadataCacheLoader());
        metadataCache.putAll(getAllMetadataFromDb());
        System.out.println(metadataCache.size());         // output is 9467 (amusing)
        System.out.println(metadataCache.asMap().size()); // output is 850  (as expected)
    }
    // assume always returns map with size equal to 850
    private static Map<MetadataCacheKey, Optional<Metadata>>  getAllMetadataFromDb(){
        return new HashMap<MetadataCacheKey, Optional<Metadata>>();
    }
}

From javadocs of Cache size:

size returns approximate number of entries in cache.

Can anybody give insights about how 850 to 9467 can be an approximation?

chammu
  • 1,275
  • 1
  • 18
  • 26
  • Possible duplicate of [Why is Cache.asMap() not consistent with Cache.size()?](http://stackoverflow.com/questions/10442846/why-is-cache-asmap-not-consistent-with-cache-size) – Gergely Bacso Nov 24 '15 at 13:35
  • I do not get my answer from above question as it talks about invalid entries in the cache being major reason. In my case I have inserted all the entries for the first time and maximum number of entries I have inserted in 850. So maximum number of invalid entries cannot be more than 850. So how come number close to 11 times of 850 is coming? – chammu Nov 24 '15 at 14:47
  • @chammu can you show us more code showing how you create and populate the cache? – Louis Wasserman Nov 24 '15 at 16:06
  • Added code. Some implementation details I have not added like getting data from database. – chammu Nov 25 '15 at 03:58
  • It's been a while now. Does the latest version of Guava have this issue? @chammu – Manu Manjunath Apr 14 '17 at 05:18
  • I would suggest trying to create an [MCVE](https://stackoverflow.com/help/mcve) demonstrating this issue so others can replicate it. In the standard `Cache` implementations [`Cache.size()`](https://github.com/google/guava/blob/fd919e5/guava/src/com/google/common/cache/LocalCache.java#L4904) and [`Cache.asMap().size()`](https://github.com/google/guava/blob/fd919e5/guava/src/com/google/common/cache/LocalCache.java#L3924) simply delegate to the same method, so what you're describing should not be possible (today). – dimo414 Dec 06 '17 at 10:28

0 Answers0