[英]How to cache data on server in asp.net mvc 4?
I am working on mvc4 web application. 我正在研究mvc4 Web应用程序。 I want to cache some database queries results and views on server side. 我想在服务器端缓存一些数据库查询结果和视图。 I used- 我用了-
HttpRuntime.Cache.Insert()
but it caches the data on client side. 但它在客户端缓存数据。 Please help. 请帮忙。
I'm using MemoryCache to store query results, and it's working fine so far. 我正在使用MemoryCache存储查询结果,到目前为止工作正常。
Here are a few links that I've used to implement it. 以下是我用来实现它的一些链接。
- Using MemoryCache in .NET 4.0 (codeproject) - 在.NET 4.0中使用MemoryCache(codeproject)
- Using MemoryCache in .NET 4.0 (blog entry) - 在.NET 4.0中使用MemoryCache(博客文章)
As I read them now, I find them not that clear, so maybe there is a better link that I've lost somewhere. 当我现在阅读它们时,我发现它们并不那么清楚,所以也许有一个更好的链接,我已经失去了某个地方。
Here is a sample of my code which I hope is clear enough so that you see how it works 这是我的代码示例,我希望它足够清晰,以便您了解它是如何工作的
public static class AgencyCacheManager
{
private static MemoryCache _cache = MemoryCache.Default;
public static List<RefAgency> ListAgency
{
get
{
if (!_cache.Contains("ListAgency"))
RefreshListAgency();
return _cache.Get("ListAgency") as List<Agency>;
}
}
public static void RefreshListAgency()
{
var listAgency = GetAllComplete();
CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddDays(1);
_cache.Add("ListAgency", listAgency, cacheItemPolicy);
}
}
And to retrieve the list from cache 并从缓存中检索列表
public Agency FindBy(string agencyId)
{
return AgencyCacheManager.ListAgency.SingleOrDefault(x => x.AgencyPartnerCode == agencyId);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.