0

The example in spring-guides for caching shows how to retrieve one element and store it the cache

@Override
@Cacheable("books")
public Book getByIsbn(String isbn) {
    simulateSlowService();
    return new Book(isbn, "Some book");
}

I would like to query the DB get the all the elements at once and then store them on startup (PostConstruct) as a reference cache.

    public Collection<Book> getAllBooks() {
    return this.entityManager.createNamedQuery(Book.all, Book.class).getResultList();
}

Can you store the Map directly? should I call the getAllBooks() method ad use the @CachePut in a BookService.

Could someone give an example/ best practice?

Thanks

  • Possible duplicate of [How to load @Cache on startup in spring?](https://stackoverflow.com/questions/27940704/how-to-load-cache-on-startup-in-spring) – tmarwen Aug 03 '17 at 11:28

1 Answers1

0

Exactly, use @CachePutin your service in order to preload the cache. Return values of methods annotated with @CachePut are always placed in the cache.

René Winkler
  • 6,508
  • 7
  • 42
  • 69