我想存储 的缓存{organizationId, userId} -> userEmail
,但我可用的 API 返回给定组织的所有电子邮件。只要我得到所有这些值,在调用期间存储它们是否安全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);
}
});