0

I have an MVC4 website and I'm using the OutputCache to cache the result of a view that displays multiple pages of ranked results. The cached output varies on the Page parameter. The rankings are a function of time, so the results for any given page can be out of sync depending on when they're cached, which is exacerbated by the fact that I'm using an infinite-scroll mechanism where duplicate results can be shown if a result gets pushed to the next page.

The ideal solution to this problem would be to cache some reasonable number of pages all at once. This would require being able to check if the cached output is expired, re-generate cached results if they are expired and then return the cached response. Is this possible?

I should also note that I'm using OutputCaching with the Azure Output caching provider, and I have a dedicated caching role (Note: not the shared caching service).

Any help would be greatly appreciated.

w.brian
  • 16,296
  • 14
  • 69
  • 118

1 Answers1

1

This would require being able to check if the cached output is expired, re-generate cached results if they are expired and then return the cached response. Is this possible?

This is exactly how OutputCaching works - request a page, if it exists in cache and isn't expired, retrieve it from cache, otherwise, render page and update the cache.

If the data really is this dynamic, you are probably causing more work/problems by caching the output without realizing any gains in performance (KISS applies here! Don't create a solution for a problem if you can avoid the problem in the 1st place).

However, to architect a solution as you describe (if really required) could be done with an Azure Queue and a Worker Role. Have your ratings engine stuff a value in the queue when a rating is added/updated. Then, have the Worker Role poll the Queue every second (for example) for values. If a value is found, the have the Worker Role do a web request against the cached page. This will update the output cache if it has expired. However, you are still limited by the cache expiration. unless you do something like from this SO post):

HttpResponse.RemoveOutputCacheItem() is probably the method you want to use. If you can figure out what name the actions are cached under, you can remove just the specific action (try setting a breakpoint or dumping all of the names of cached items to the screen)

Community
  • 1
  • 1
viperguynaz
  • 12,044
  • 4
  • 30
  • 41
  • I realize that's how OutputCaching, but it's all done behind the scenes. Like I said, in order to page through say, 10 pages of results without duplicate results, all 10 pages would have had to be generated at the same time using the same resultset. – w.brian Mar 13 '13 at 01:36