Google Guava CacheBuilder - Issue With Bulk Load From MongoDB Collection
<p>I'm trying to override Google Guava's CacheLoader loadAll() method. I'm wanting to do a bulk lookup and retrieve all the documents in the Employee collection that were not specifically requested like it states in <a href="https://github.com/google/guava/wiki/CachesExplained" rel="nofollow noreferrer">CachesExplained</a>:</p>

<blockquote>
  <p>Note that you can write a CacheLoader.loadAll implementation that loads values for keys that were not specifically requested. For example, if computing the value of any key from some group gives you the value for all keys in the group, loadAll might load the rest of the group at the same time.</p>
</blockquote>

<p>I've scoured the internet and the only post I've found that was remotely related to my issue was <a href="https://stackoverflow.com/questions/11794297/google-guavas-cacheloader-loadall-method-implementation-issues">Google Guava's CacheLoader loadAll() method Implementation Issues</a>. However, I still cannot bulk retrieve all the documents from my collection.
Whenever I run my code, the only values retrieved from the database are the ones that were specifically passed in via a List.</p>

<p>Let's say I have a Mongo collection name Employees with 3 employee documents in it:</p>

<pre><code>{"_id" : "123", "John Doe", "CEO"}
{"_id" : "456", "Jane Doe", "President"}
{"_id" : "789", "Jimmy Doe", "CFO"}
</code></pre>

<p>Employee class:</p>

<pre><code>public class Employee extends MongoDbModel {

    @Id
    private final String employeeNumber;

    private final String name;

    private final String type;

    public Employee(String employeeNumber, String name, String type){
        this.employeeNumber = employeeNumber;
        this.name = name;
        this.type = type;

    }

    public String getEmployeeNumber() {
        return employeeNumber;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public static LoadingCache&lt;String, Employee&gt; employeeCacheMap;
    static {
        employeeCacheMap = CacheBuilder.newBuilder()
                .concurrencyLevel(4)
                .weakKeys()
                .maximumSize(10000)
                .expireAfterWrite(5, TimeUnit.MINUTES)
                .build(
                        new CacheLoader&lt;String, Employee&gt;() {
                            @Override
                            public Employee load(String key) throws ExecutionException{
                                return getOneFromDatabase(key);
                            }
                            @Override
                            public Map&lt;String, Employee&gt; loadAll(Iterable&lt;? extends String&gt; key){
                                return getAllEmployeesFromDatabase();
                            }
                        }
                );
    }

    private static Employee getOneFromDatabase(String employeeNumber){
        return MongoDbStore.createQuery(Employee.class).filter("_id", employeeNumber).first();
    }

    private static Map&lt;String, Employee&gt; getAllEmployeesFromDatabase(){
        final List&lt;Employee&gt; list = MongoDbStore.createQuery(Employee.class).asList();

        final Map&lt;String, Employee&gt; map = new HashMap&lt;String,Employee&gt;(list.size());
        for(final Employee employee : list){
            map.put(employee.getName(), employee);
        }
        return map;
    }

    public static LoadingCache&lt;String, Employee&gt; getLoadingCache(){
        return employeeCacheMap;
    }
}
</code></pre>

<p>Whenever I run this method:</p>

<pre><code>public void getAllEmployees(){
    List&lt;String&gt; myList = new ArrayList&lt;&gt;();
    myList.add("123");
    myList.add("456");
    Map&lt;String, Employee&gt; employeeMap = null;
    try {
        employeeMap = Employee.getLoadingCache().getAll(myList);
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    for(Employee employee : employeeMap.values()){
        System.out.println(employee.getName());
    }
}
</code></pre>

<p>It only prints out:</p>

<pre><code>"John Doe"
"Jane Doe"
</code></pre>

<p>instead of all three names as described in the above quote from <a href="https://github.com/google/guava/wiki/CachesExplained" rel="nofollow noreferrer">CachesExplained</a>.</p>

<p>Am I misunderstanding the above quote and the CacheLoader only retrieves the values that are passed into the loadAll() method instead of retrieving all of the values from the collection?
Any help will be much appreciated</p>