ASP.NET MVC,自动完成文本框,缓存?

时间:2022-01-22 05:12:11

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

使用ASP.NET MVC,我实现了一个自动完成文本框,使用的方法非常类似于Ben Scheirman的实现,如下所示:http://flux88.com/blog/jquery-auto-complete-text-box-with- ASP净MVC /

What I haven't been able to figure out is if it's a good idea to cache the data for the autocomplete textbox, so there won't be a roundtrip to the database on every keystroke?

我无法弄清楚的是,为自动填充文本框缓存数据是否是一个好主意,因此每次击键时都不会有数据库往返?

If caching is prefered, can you guide me in the direction to how to implement caching for this purpose?

如果首选缓存,您是否可以指导我如何为此目的实现缓存?

4 个解决方案

#1


You have a couple things to ask yourself:

你有几件事要问自己:

  1. Is the data I'm pulling back dynamic?
  2. 我正在撤回动态的数据吗?

  3. If not, how often do I expect this call to occur?
  4. 如果没有,我多久会发生一次此呼叫?

If the answers are, 1- not really and 2 - call to happen frequently, you should cache it.

如果答案是,1-不是真的和2 - 经常发生呼叫,你应该缓存它。

I don't know how your data access is setup, but I simply throw my data into cache objects like so:

我不知道您的数据访问是如何设置的,但我只是将数据抛出到缓存对象中,如下所示:

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 repository queries, based off of LINQ to SQL. It first checks the cache, if the entry exists in cache, it returns it. If not, it goes to the database, then caches it with a sliding expiration.

这是我的一个存储库查询的示例,基于LINQ to SQL。它首先检查缓存,如果该条目存在于缓存中,则返回它。如果没有,它将进入数据库,然后以滑动过期缓存它。

#2


You sure can Cache your result, using the attribute like:

您确定可以使用以下属性缓存结果:

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

ASP.net will handle the rest.

ASP.net将处理其余的事情。

#3


I think caching in this case would require more work than simply storing every request. You'd want to focus more on the terms being searched than individual keys. You'd have to keep track of what terms are more popular and cache combinations of characters that make up those terms. I don't think simply caching every single request is going to get you any performance boost. You're just going to have stale data in your cache.

我认为在这种情况下缓存比简单地存储每个请求需要更多的工作。您希望更多地关注搜索的术语而不是单个键。您必须跟踪哪些术语更受欢迎,并缓存组成这些术语的字符组合。我不认为简单地缓存每一个请求都会让你获得任何性能提升。您只需要在缓存中包含陈旧数据。

#4


Well, how will caching in asp.net prevent server round trips? You'll still have server round trips, at best you will not have to look up the database if you cache. If you want to prevent server roundtrips then you need to cache at the client side.

那么,asp.net中的缓存将如何防止服务器往返?您仍然可以进行服务器往返,最好不要在缓存时查找数据库。如果要阻止服务器往返,则需要在客户端缓存。

While it's quite easily possible with Javascript (You need to store your data in a variable and check that variable for relevant data before looking up the server again) I don't know of a ready-tool which does this for you.

虽然使用Javascript很容易实现(您需要将数据存储在变量中并在再次查找服务器之前检查该变量以获取相关数据)但我不知道为您执行此操作的现成工具。

I do recommend you consider caching to prevent round-trips. In fact I have half a mind to implement javascript caching in one of my own websites reading this.

我建议你考虑缓存以防止往返。事实上,我有一半想在我自己的网站上阅读本文实现javascript缓存。

#1


You have a couple things to ask yourself:

你有几件事要问自己:

  1. Is the data I'm pulling back dynamic?
  2. 我正在撤回动态的数据吗?

  3. If not, how often do I expect this call to occur?
  4. 如果没有,我多久会发生一次此呼叫?

If the answers are, 1- not really and 2 - call to happen frequently, you should cache it.

如果答案是,1-不是真的和2 - 经常发生呼叫,你应该缓存它。

I don't know how your data access is setup, but I simply throw my data into cache objects like so:

我不知道您的数据访问是如何设置的,但我只是将数据抛出到缓存对象中,如下所示:

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 repository queries, based off of LINQ to SQL. It first checks the cache, if the entry exists in cache, it returns it. If not, it goes to the database, then caches it with a sliding expiration.

这是我的一个存储库查询的示例,基于LINQ to SQL。它首先检查缓存,如果该条目存在于缓存中,则返回它。如果没有,它将进入数据库,然后以滑动过期缓存它。

#2


You sure can Cache your result, using the attribute like:

您确定可以使用以下属性缓存结果:

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

ASP.net will handle the rest.

ASP.net将处理其余的事情。

#3


I think caching in this case would require more work than simply storing every request. You'd want to focus more on the terms being searched than individual keys. You'd have to keep track of what terms are more popular and cache combinations of characters that make up those terms. I don't think simply caching every single request is going to get you any performance boost. You're just going to have stale data in your cache.

我认为在这种情况下缓存比简单地存储每个请求需要更多的工作。您希望更多地关注搜索的术语而不是单个键。您必须跟踪哪些术语更受欢迎,并缓存组成这些术语的字符组合。我不认为简单地缓存每一个请求都会让你获得任何性能提升。您只需要在缓存中包含陈旧数据。

#4


Well, how will caching in asp.net prevent server round trips? You'll still have server round trips, at best you will not have to look up the database if you cache. If you want to prevent server roundtrips then you need to cache at the client side.

那么,asp.net中的缓存将如何防止服务器往返?您仍然可以进行服务器往返,最好不要在缓存时查找数据库。如果要阻止服务器往返,则需要在客户端缓存。

While it's quite easily possible with Javascript (You need to store your data in a variable and check that variable for relevant data before looking up the server again) I don't know of a ready-tool which does this for you.

虽然使用Javascript很容易实现(您需要将数据存储在变量中并在再次查找服务器之前检查该变量以获取相关数据)但我不知道为您执行此操作的现成工具。

I do recommend you consider caching to prevent round-trips. In fact I have half a mind to implement javascript caching in one of my own websites reading this.

我建议你考虑缓存以防止往返。事实上,我有一半想在我自己的网站上阅读本文实现javascript缓存。