title: Pre-populating a cache with Spring's JSR-107 annotations
body: <p>I have a table with often-read, rarely-changed records (about 50,000) that I need to cache. I'd like to pre-populate the cache at server startup time with a single query loading all records. I'm not sure what's the best way to do it when using Spring's <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html#cache-jsr-107" rel="nofollow">annotations</a>. The only way I found apparent from the docs is to load all entities in one service method than pass them to a method in another service annotated with <code>@CachePut</code> and which doesn't do anything else.</p>
  
  <pre><code>PreloaderService:
  
  @AutoWired
  private CacheService cacheService;
  
  public void getAllWidgets() {
    List&lt;Widget&gt; widgets = ... load them in one query
    for (Widget widget: widgets) {
      cacheService.populate(widget); //must call a different class so that it Spring's proxy for @CachePut gets used
    }
  }
  
  
  CacheService:
  
  @CachePut
  public void populate(@CacheValue Widget widget){
    //do nothing else, all we need is the caching annotations to be processed
  }
  </code></pre>
  
  <p>The above doesn't appear very elegant to me but cannot see another way. By the way, I'm using Ehcache in case a lower-level API would achieve this.</p>