Asp.net Page.Cache与HttpContext.Current.Cache
我读过一个web应用程序的源代码,看到它使用缓存对象(web.Caching.Cache)缓存数据。在代码隐藏文件(aspx.cs文件)中,它使用Page.Cache获取缓存,而在其他类定义文件中,它使用HttpContext.Current.Cache获取缓存。我想知道为什么它不使用相同的选项来获取缓存。有人能解释一下Page.Cache和HttpContext.Current.Cache之间的区别吗?为什么要在上面的每个上下文中使用每一个呢。我可以对上述两种上下文使用Page.Cache或HttpContext.Current.Cache吗?Asp.net Page.Cache与HttpContext.Current.Cache,asp.net,caching,Asp.net,Caching,我读过一个web应用程序的源代码,看到它使用缓存对象(web.Caching.Cache)缓存数据。在代码隐藏文件(aspx.cs文件)中,它使用Page.Cache获取缓存,而在其他类定义文件中,它使用HttpContext.Current.Cache获取缓存。我想知道为什么它不使用相同的选项来获取缓存。有人能解释一下Page.Cache和HttpContext.Current.Cache之间的区别吗?为什么要在上面的每个上下文中使用每一个呢。我可以对上述两种上下文使用Page.Cache或H
提前感谢。没有区别,前者使用当前页面实例,后者使用
静态方法,通过该方法也可以在没有页面实例的静态方法中工作
两者都引用相同的应用程序缓存
因此,您可以通过页面
获取缓存
,例如在页面加载
:
protected void Page_load(Object sender, EventArgs e)
{
System.Web.Caching.Cache cache = this.Cache;
}
或者在静态方法中(在HttpContext
中使用),通过HttpContext.Current
:
static void Foo()
{
var context = HttpContext.Current;
if (context != null)
{
System.Web.Caching.Cache cache = context.Cache;
}
}
谢谢你的回答。这对我很有用。