1

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 annotations. 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 @CachePut and which doesn't do anything else.

PreloaderService:

@AutoWired
private CacheService cacheService;

public void getAllWidgets() {
  List<Widget> 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
}

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.

wishihadabettername
  • 14,231
  • 21
  • 68
  • 85
  • I think this can be closed as a duplicate http://stackoverflow.com/questions/26927044/how-to-load-data-from-database-to-ehcache-when-the-application-starts – Leon May 11 '15 at 13:58
  • Here is a blog with exactly what you want https://javaglobin.wordpress.com/category/spring/ – Leon May 11 '15 at 14:06
  • Thanks @Leon, I had missed the other resources. You can put it as an answer, I'll accept it. – wishihadabettername May 11 '15 at 15:21
  • nah, if you are happy with that other answer I will add the blog link to that and close this one as a duplicate – Leon May 11 '15 at 15:22

0 Answers0