如何缓存在Asp.net C#MVC数据库表prevent许多数据库查询 [英] How to cache database tables to prevent many database queries in Asp.net C# mvc
问题描述
我使用Asp.net MVC 4(C#)建立自己的CMS,我想缓存某些数据库的数据,喜欢:本土化,搜索类(它的长尾巴,每一类有它自己的子和子子类)等。
I build my own cms using Asp.net mvc 4 (c#), and I want to cache some database data, likes: localization, search categories (it's long-tail, each category have it's own sub and sub-sub categories), etc..
这将是矫枉过正查询数据库所有的时间,因为它可以为每个页面请求超过30-100查询,但是用户很少更新这些数据库
It's will be overkill to query the database all the time, because it can be more than 30-100 queries for each page request, however the users update those database rarely
那么,什么是做到这一点的最佳方式(性能和便利性)? (对于指南和教程的链接将是不错的也可以)
我知道如何使用动作的OutputCache,但它不是我需要在这种情况下,它的高速缓存中的HTML,但我需要的是例如,我自己的助手 @ html.Localization (Newsletter.Tite)
将采取语言与数据交互等的价值,或任何其他的帮手。
I know how use the OutputCache of the action, but it's not what I need in this situation , it's cache the html, but what I need is for example, that my own helper @html.Localization("Newsletter.Tite")
will take the value of the language, or any another helper that interact with data etc.
我觉得(不是很确定),我需要缓存我想要的数据,只有当应用程序调用是在第一时间,然后与缓存的位置工作,但我没有任何经验,甚至如何吧。
I think (not really sure) that I need to cache the data I want, only when the application is invoke for the first time, and then work with the cache location, but I don't have any experience even how to it.
推荐答案
您可以使用内置的<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx\"><$c$c>MemoryCache$c$c>存储已经从数据库中检索结果集全
You could use the built-in MemoryCache
to store entire resultsets you have retrieved from the database.
一个典型的模式:
MyModel model = MemoryCache.Default["my_model_key"] as MyModel;
if (model == null)
{
model = GetModelFromDatabase();
MemoryCache.Default["my_model_key"] = model;
}
// you could use the model here
这篇关于如何缓存在Asp.net C#MVC数据库表prevent许多数据库查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!