我有 asp.net mvc web 应用程序,使用 .net 3.5
我想在 UI 逻辑层使用缓存。
我读过
1- 缓存类
http://msdn.microsoft.com/en-us/library/system.web.caching.cache(v=vs.90).aspx
2- 使用 HTTP header 进行缓存
http://www.dotnetperls.com/cache
我不确定有什么区别以及我应该使用哪一个。
此外,如何在它们中的每一个中配置缓存?
项目 1- 仅在网络配置中?
第 2 项 - 仅以编程方式?
更新:
我试过了
使用 System.Web.Caching;
private string GetTitlePerBDataId(Guid changeRequestDataId)
{
var key = string.Format("{0}_{1}", TITLE, changeRequestDataId);
if (System.Web.Caching.Cache[key] == null)
{
Cache[key] = mBundlatorServiceHelper.GetData(changeRequestBundleDataId).Title;
}
return Convert.ToString(Cache[key]);
}
但是在这一点上得到的类名是无效的 Cache
请您参考如下方法:
Cache 类在内存缓存中,在服务器上。 您可以在那里缓存对象和其他内容。
使用 http header 进行缓存,定义客户端/代理如何缓存输出。
如果您查看 System.Web.Caching.Cache 的文档,它说
Information about an instance of this class is available through the Cache property of the HttpContext object or the Cache property of the Page object.
所以你只能通过 httpcontext 使用它。
private string GetTitlePerBDataId(Guid changeRequestDataId)
{
var key = string.Format("{0}_{1}", TITLE, changeRequestDataId);
if (System.Web.HttpContext.Current.Cache[key] == null)
{
System.Web.HttpContext.Current.Cache.Insert(key, mBundlatorServiceHelper.GetData(changeRequestBundleDataId).Title);
}
return Convert.ToString(System.Web.HttpContext.Current.Cache[key]);
}
