我有asp.net mvc web应用程序,使用.net 3.5
我想在UI Logic层中使用缓存。
我读到了
1- Cache Class
http://msdn.microsoft.com/en-us/library/system.web.caching.cache(v=vs.90).aspx
2- Caching with HTTP headers
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]);
}
But got class name is not valid in this point
超过Cache
答案 0 :(得分:4)
Cache类位于服务器上的内存缓存中。 你可以在那里缓存对象和其他东西。
使用http标头进行缓存,定义客户端/代理如何缓存输出。
如果您查看System.Web.Caching.Cache的文档,则说
有关此类实例的信息可通过 HttpContext对象的Cache属性或者的Cache属性 页面对象。
所以你只能通过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]);
}