private void resolveFromCache(FormScanBatch batch) { Set<String> toFetch = new HashSet<>(); // Collect the keys we need from all enqueued tables for (FormScan formScan : batch.getScans()) { toFetch.addAll(formScan.getCacheKeys()); } if (!toFetch.isEmpty()) { Map<String, Object> cached = cache.getAll(toFetch); // Now populate the individual collection scans with what we got back from memcache // with a little luck nothing will be left to query directly from the database for (FormScan formScan : batch.getScans()) { formScan.updateFromCache(cached); } } }