4

In a Java EE application built with the Spring Framework I need to do some really expensive operations in a DAO that can take up to several minutes. Using Spring MVC, I hit the DAO through my Controller, when the user's request maps to the controller method:

@RequestMapping(value = "/categories.do")
public ModelAndView categories(
        @PathVariable("cc") String cc,
        @PathVariable("ll") String ll) {
    Locale locale = new Locale(ll, cc);
    ModelAndView result = getView("categories", locale);
    Map<Category, List<Product>> allProducts = supplyDao.getAllProducts(locale);
    result.addObject("products", allProducts);
    return result;
}

The getAllProducts method makes a large number of requests to an external web service to draw all the data needed for the products. The method is cached via Spring's @Cacheable annotation, over an underlying Ehcache implementation:

@Cacheable(value = CACHE_NAME, key = CACHE_KEY_PREFIX + "'(' + #p0 + ')'")
public Map<Category, List<Product>> getAllProducts(Locale locale) {
        // a lot of HTTP requests firing from here
}

The problem with this approach is that while the cache is empty the page is basically unaccessible. Moreover, if more than one request hits the page when the cache is empty, the DAO method will fire again and all the requests would be repeated in parallel. As far as I understand, a solution to the second problem would be to use a BlockingCache, but I haven't had the chance to implement it yet.

What I want is to have the controller method always draw its results from the cache. I want to implement a @PostConstruct method that triggers cache population for all locales. Something like:

@PostConstruct
public void populateCaches() {
    for (Locale locale : localeList) {
        getAllProducts(locale);
    }
}

I don't mind the initial population taking a while since the server is seldomly restarted. I also will set the cache expiration to something like three days - the data does not get updated frequently and there is no danger in not serving the most up to date version.

What I would like to do is run a TimerTask at set intervals, let's say two days and 23 hours, which would force a DAO method to draw all the product data from the web services. This data would then replace the data in the cache, without it having been expired. The cache expiration counter would then be reset - the data would expire in three days time again. This way, the controller method would always get the product data from the cache and the page would be responsive.

My question is, how would I implement such a method given that I am using Spring's cache abstraction? Do I need to handle the CacheManager directly in my methods?

Another question is: am I approaching the problem correctly? Is there a better way of doing this?

Community
  • 1
  • 1
Alex Ciminian
  • 11,398
  • 15
  • 60
  • 94

1 Answers1

7

To block calls until a cache is available

@Cacheable(cacheName="yourCache", decoratedCacheType= DecoratedCacheType.SELF_POPULATING_CACHE)
public List<String> getWhatever(int id) {
//call database
}

Instead of a timertask you could use an autorefreshing cache :

@Cacheable(cacheName="yourCache", refreshInterval=1000, decoratedCacheType= DecoratedCacheType.REFRESHING_SELF_POPULATING_CACHE)
public List<String> getWhatever(int id) {
  //call database
}

kudos

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • 2
    just to clarify, this is using the ehcache-spring-annotations, not the built in @Cacheable in spring proper- – chrismarx Jul 16 '14 at 18:48