Search code examples
c#asp.netajaxcachingautocompleteextender

Stop Multiple Calls to DB from AutoCompleteExtender


I can't seem to find much help on this. I'm using the autocomplete extender in .net to suggest options for search box when user adds keystrokes to search textbox. This works fine, although a new call to the DB is sent every time a key is pressed.

This is fine until the number of website users start to get high! So I used the cache with datatable.select instead of making a round trip to the DB everytime.

If the cache does not exist, it is created, and then everytime after that the cache is used.

However, the issue now is that all the users that arrive while the cache is being created by the first visitor assume there is no cache and start trying to create their own cache of the same name, thereby overloading the DB again!

Are there any pointers on the best way to get round this?

Thanks, Ben


Solution

  • Depending how static your data is, you could prime the cache in application_start or some place like that. (Like @pinoy_ISF suggested).

    You could also set an application-level flag, indicating whether you are currently building the cache, and if so, go directly to the DB without updating the cache. Once you have completed building the cache, revert the flag, and subsequent requests should hit the cache normally.