1

アプリケーションでグアバキャッシュを使用してパフォーマンスを向上させています。LoadingCacheのputAll API を使用して、キャッシュに 850 エントリを挿入しました。設定した最大キャッシュ サイズは 20K エントリです。テストクラス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>>();
    }
}

キャッシュサイズのjavadocから:

size は、キャッシュ内のエントリのおおよその数を返します。

850 から 9467 がどのように近似値になるかについて、誰か洞察を与えることができますか?

4

0 に答える 0