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.