I want to store a cache of {organizationId, userId} -> userEmail
, but the API available to me returns all emails for a given organization. So long as I'm getting all of these values, is it safe to store them all during a call to CacheLoader::load
?
private final LoadingCache<Pair<UUID, UUID>, String> emailCache = CacheBuilder
.newBuilder()
.maximumSize(10000)
.build(new CacheLoader<Pair<UUID, UUID>, String>() {
@Override
public String load(final Pair<UUID, UUID> key) throws Exception {
final UUID orgId = key.getValue0();
final List<User> users = remoteService.getAllUsers(orgId);
final Map<Pair<UUID, UUID>, String> updates = new HashMap<>();
for (User user : users) {
updates.put(Pair.with(orgId, user.getId()), user.getEmail());
}
// is this safe?
emailCache.putAll(updates);
return updates.get(key);
}
});
No, it isn't, as that can cause races. On the other hand, it is safe to do this with CacheLoader.loadAll, which specifically documents that it can return a map with more entries than were requested.