ASP.NET MVC, text field autocomplete, caching?

Using ASP.NET MVC, I applied an autocomplete text box using an approach very similar to the Ben Scheirman implementation, as shown here: http://flux88.com/blog/jquery-auto-complete-text-box-with-asp- net-mvc /

What I was not able to understand is a good idea to cache data for an autocomplete text field, so there will be no rounding to the database every time you press a key?

If caching is preferable, can you guide me towards implementing caching for this purpose?

+4
source share
4 answers

You have a couple of questions to ask yourself:

  • Am I returning dynamic data data?
  • If not, how often do I expect this call to occur?

If there are answers, 1- not really and 2- the call often happens, you should cache it.

I donโ€™t know how your data access is configured, but I just throw my data into cache objects as follows:

public IQueryable<Category> FindAllCategories() { if (HttpContext.Current.Cache["AllCategories"] != null) return (IQueryable<Category>)HttpContext.Current.Cache["AllCategories"]; else { IQueryable<Category> allCats = from c in db.Categories orderby c.Name select c; // set cache HttpContext.Current.Cache.Add("AllCategories", allCats, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 30, 0, 0), System.Web.Caching.CacheItemPriority.Default, null); return allCats; } } 

This is an example of one of my queries to a repository based on LINQ to SQL. First he checks the cache, if the entry exists in the cache, it returns it. If not, it enters the database and then caches it with an expiration date.

+5
source

Are you sure you can cache your result using an attribute like:

 [OutputCache(Duration=60, VaryByParam="searchTerm")] 

ASP.net will handle the rest.

+5
source

I think caching in this case will require more work than just saving each request. You want to focus more on search queries than on individual keys. You will need to keep track of which conditions are more popular and combine with the cache of combinations of characters that make up these terms. I do not think that simply caching each request will lead to an increase in performance. You will have outdated data in your cache.

0
source

Well, how does caching in asp.net prevent round trips? You will still have server rides; at best, you wonโ€™t have to search the database if you cache. If you want to prohibit server transitions, you need to cache on the client side.

While this is possible with Javascript (you need to store the data in a variable and check this variable for the corresponding data before looking for the server again). I do not know a ready-made tool that will do this for you.

I recommend that you consider caching to prevent round trips. In fact, I have half the mind to implement javascript caching on one of my own sites reading this.

-1
source

All Articles