Search in sources :

Example 1 with CacheLoader

use of org.elasticsearch.common.cache.CacheLoader in project elasticsearch by elastic.

the class IndicesRequestCache method getOrCompute.

BytesReference getOrCompute(CacheEntity cacheEntity, Supplier<BytesReference> loader, DirectoryReader reader, BytesReference cacheKey) throws Exception {
    final Key key = new Key(cacheEntity, reader.getVersion(), cacheKey);
    Loader cacheLoader = new Loader(cacheEntity, loader);
    BytesReference value = cache.computeIfAbsent(key, cacheLoader);
    if (cacheLoader.isLoaded()) {
        key.entity.onMiss();
        // see if its the first time we see this reader, and make sure to register a cleanup key
        CleanupKey cleanupKey = new CleanupKey(cacheEntity, reader.getVersion());
        if (!registeredClosedListeners.containsKey(cleanupKey)) {
            Boolean previous = registeredClosedListeners.putIfAbsent(cleanupKey, Boolean.TRUE);
            if (previous == null) {
                ElasticsearchDirectoryReader.addReaderCloseListener(reader, cleanupKey);
            }
        }
    } else {
        key.entity.onHit();
    }
    return value;
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) CacheLoader(org.elasticsearch.common.cache.CacheLoader)

Aggregations

BytesReference (org.elasticsearch.common.bytes.BytesReference)1 CacheLoader (org.elasticsearch.common.cache.CacheLoader)1