0

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);
            }
        });
Andrew Rueckert
  • 4,858
  • 1
  • 33
  • 44
  • Maybe instead use the orgId as the key and an immutable map as the value. Then use a weigher based on the map's size. This will let you keep the bulk items, but evict the whole map and may consume too much space for low valued mappings. – Ben Manes Jul 18 '17 at 16:01

1 Answers1

2

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.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413