Search code examples
asp.net-mvc-3asynchronousasynccontroller

MVC3 - using AsyncController to prepopulate ObjectCache from database


I have a form that searches via AJAX against two different data sources. The data is relatively small but the speed at which it returns is slow.

I built a cache layer to store the full result after the first query... however, I would like to prime the cache with data before the user executes the search.

Should I be looking at an AsyncController to do this? Any recommendations?

My desired behavior is (updated):

  1. User requests any ActionABC of some controller (not necessarily the search action)
  2. Server-side, that action checks the cache and asynchronously requests data if empty
  3. ActionABC returns requested view while cache continues to populate on server
  4. If the user subsequently performs a search while cache being populated, their request waits until cache populate is complete otherwise cache data is immediately available

Solution

  • I ended up not having to use AsyncControllers.

    I used the Task Factory to "fire and forget" a call to load the data initially upon any call to the controller.

    Task.Factory.StartNew(() => { var x = GetData(); });
    

    Inside "GetData" call I used LOCK to force subsequent calls to wait until cache was populated (addresses #4)

    private static object ThisLock = new object();
    
    protected MyData GetData()
    {
      if(<MyData in cache>)
        return MyData from cache;
    
      lock(ThisLock)
      {
        // just entered lock, see if cache was set by previous blocking thread
        if(MyData in cache>)
          return data from cache;
    
        ... load MyData from database ... 
    
        ... save MyData  to cache ... 
    
        return MyData from cache;
      }
    }