Skip to content

Example For Bulk Load #588

Answered by ben-manes
gyansatapathy asked this question in Q&A
Sep 3, 2021 · 2 comments · 2 replies
Discussion options

You must be logged in to vote

Hi @gyansatapathy,

You can simply implement the method and the cache will invoke it on a bulk request.

Cache<UUID, User> users = Caffeine.newBuilder().build(new CacheLoader<UUID, User>() {
  public User load(UUID id) {
    return repository.getUserById(id);
  }
  public Map<UUID, User> loadAll(Set<UUID> ids) {
    return repository.getAllUsersById(ids);
  }
});

cache.get(Set.of(UUID.randomUUID(), UUID.randomUUID()));

You might also find the CacheLoader.bulk convenience function useful as a shorter syntax that delegates load(key) to the loadAll(keys) method.

Cache<UUID, User> users = Caffeine.newBuilder()
    .build(CacheLoader.bulk((Set<UUID> keys) -> repository.getAllUsersById(ids));

c…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@gyansatapathy
Comment options

@ben-manes
Comment options

Answer selected by ben-manes
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants