Tom*_*sen 4 asp.net-mvc caching autocomplete
使用ASP.NET MVC,我实现了一个自动完成文本框,使用的方法非常类似于Ben Scheirman的实现,如下所示:http://flux88.com/blog/jquery-auto-complete-text-box-with- ASP净MVC /
我无法弄清楚的是,为自动填充文本框缓存数据是否是一个好主意,因此每次击键时都不会有数据库往返?
如果首选缓存,您是否可以指导我如何为此目的实现缓存?
你有几件事要问自己:
如果答案是,1-不是真的和2 - 经常发生呼叫,你应该缓存它.
我不知道您的数据访问是如何设置的,但我只是将数据抛出到缓存对象中,如下所示:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的一个存储库查询的示例,基于LINQ to SQL.它首先检查缓存,如果该条目存在于缓存中,则返回它.如果没有,它将进入数据库,然后以滑动过期缓存它.
您确定可以使用以下属性缓存结果:
[OutputCache(Duration=60, VaryByParam="searchTerm")]
Run Code Online (Sandbox Code Playgroud)
ASP.net将处理其余的事情.
| 归档时间: |
|
| 查看次数: |
5088 次 |
| 最近记录: |