.net中的缓存的访问方式
2009-06-01 09:53
225 查看
在.net中引入了Cache借以提高程序执行效率.在程序里使用使用这Cache时可以使用三种方式:
HttpRuntime.Cache
Page.Cache
HttpContext.Current.Cache
我们主要讨论这三种方式到底有什么不同.
首先,在查看Cache的定义时,可以发现,这三个类的Cache是同一个东西.这就表示,不论你使用那种方法在服务器端建立一个缓存,利用其它两种方式都是可以访问到的.实践一下就知道了.
首先,在一页面设置缓存,如下代码
if ( Page.Cache["PageCache"] == null )
{
Page.Cache.Insert("PageCache","This is PageCache") ;
Response.Write( "设置成功!<br>" ) ;
}
if ( HttpContext.Current.Cache["HttpCache"] == null )
{
HttpContext.Current.Cache.Insert("HttpCache","This is HttpCache");
Response.Write( "设置成功!<br>" ) ;
}
if ( HttpRuntime.Cache["HttpRuntimeCache"] == null )
{
HttpRuntime.Cache.Insert("HttpRuntimeCache" , "This is HttpRuntimeCache" ) ;
Response.Write( "设置成功!<br>" ) ;
}
然后,在另一页面通过不同的方式来访问这些已经设置过的缓存.如下代码:
Response.Write( "第一部分:<br>" ) ;
Response.Write( "----利用HttpContext.Current返回的HttpContext访问通过Page.Cache设置的缓存:" ) ;
if ( HttpContext.Current.Cache["PageCache"] != null )
{
Response.Write( "其值为:" +HttpContext.Current.Cache["PageCache"].ToString() +"<br>") ;
}
else
{
Response.Write( "没有访问到!<br>") ;
}
Response.Write( "----利用HttpRuntime.Cach访问通过Page.Cache设置的缓存:" ) ;
if ( HttpRuntime.Cache["PageCache"] != null )
{
Response.Write( "其值为:" +HttpRuntime.Cache["PageCache"].ToString() +"<br>") ;
}
else
{
Response.Write( "没有访问到!<br>") ;
}
Response.Write( "第二部分:<br>" ) ;
Response.Write( "----利用HttpContext.Current返回的HttpContext访问通过HttpRuntime设置的缓存:" ) ;
if ( HttpContext.Current.Cache["HttpRuntimeCache"] != null )
{
Response.Write( "其值为:" +HttpContext.Current.Cache["HttpRuntimeCache"].ToString() +"<br>") ;
}
else
{
Response.Write( "没有访问到!<br>") ;
}
Response.Write( "----利用Page.Cach访问通过HttpRuntime.Cache设置的缓存:" ) ;
if ( Page.Cache["HttpRuntimeCache"] != null )
{
Response.Write( "其值为:" +Page.Cache["HttpRuntimeCache"].ToString() +"<br>") ;
}
else
{
Response.Write( "没有访问到!<br>") ;
}
Response.Write( "第三部分:<br>" ) ;
Response.Write( "----利用HttpRuntime.Cache访问通过HttpContext.Current.Cache设置的缓存:" ) ;
if ( HttpRuntime.Cache["HttpCache"] != null )
{
Response.Write( "其值为:" +HttpRuntime.Cache["HttpCache"].ToString() +"<br>") ;
}
else
{
Response.Write( "没有访问到!<br>") ;
}
Response.Write( "----利用Page.Cach访问通过HttpContext.Current.Cache设置的缓存:" ) ;
if ( Page.Cache["HttpCache"] != null )
{
Response.Write( "其值为:" +Page.Cache["HttpCache"].ToString() +"<br>") ;
}
else
{
Response.Write( "没有访问到!<br>") ;
}
执行的结果与之前的设想是一样的.那么这几种访问缓存的方式到底有什么不一样呢?看下面的代码!
方法一:访问HttpContext.Current.Cache
private void OutHttpContextCacheToString()
{
try
{
if ( HttpContext.Current.Cache["HttpCache"] != null )
{
Response.Write( HttpContext.Current.Cache["HttpCache"].ToString() + "<br>" ) ;
}
else
{
Response.Write( "没有访问到缓存或缓存没有设置!<br>" ) ;
}
}
catch
{
Response.Write( "发生错误!<br>" ) ;
}
}
方法二:访问Httpruntime.Cache
private void OutHttpRumtimeCacheToString()
{
if ( HttpRuntime.Cache["HttpRuntimeCache"] != null )
{
Response.Write( HttpRuntime.Cache["HttpRuntimeCache"].ToString() + "<br>" ) ;
}
else
{
Response.Write( "没有访问到缓存或缓存没有设置!<br>" ) ;
}
}
上面的两个方法是通过两种不同的方式去读取缓存.如果你是在程里直接使用的话,程序是正常的.如果利用线程去调用这两个方法来读取缓存的话,会有什么结果呢?见下的代码:
Response.Write( "利用线程通过HttpContext.Current.Cache来访问缓存中的数据,访问结果为:<br>" ) ;
Thread TestThreadHttpContext = new Thread( new ThreadStart( OutHttpContextCacheToString ) ) ;
TestThreadHttpContext.Priority = ThreadPriority.Highest ;
TestThreadHttpContext.Start() ;
Response.Write( "利用线程通过HttpRuntime.Cache来访问缓存中的数据,访问结果为:<br>" ) ;
Thread TestThreadHttpRuntime = new Thread( new ThreadStart( OutHttpRumtimeCacheToString ) ) ;
TestThreadHttpRuntime.Priority = ThreadPriority.Highest ;
TestThreadHttpRuntime.Start() ;
在查看结果的时候,你会发现,在执行过程中有错误发生了(通过跟踪代码也可以发现错误).错误发生在线程调用OutHttpContextCacheToString()方法中.而通过线程去调OutHttpRumtimeCacheToString()方法时却一切正常.为什么通过线程去调用OutHttpContextCacheToString()方法时会发生错误呢?这其实就是两种HttpRumtime与HttpContext访问缓存的不同之处.
查看微软的MSDN文档,HttpContext的定义为"封装有关个别 HTTP 请求的所有 HTTP 特定的信息",而HttpRuntime的定义为"为当前应用程序提供一组 ASP.NET 运行时服务",换句话说,一个为是与特定Http请求相关联的,一个是与整个应用程序相关联.找到问题的的在了吗?
我们知道,每一个Http请求都会产生一个HttpContext的实例,该实例可以通过HttpContext的Current属性来获取.该实例包含了当前Http请求的相关上下文信息.如果你对程序进行跟踪,你就会发现,当利用新线程访问HttpContext.Current.Cache时,程序报错,错误并不是发生在Cache,而是发生在HttpContext.Current,因为HttpContext.Current这个属性此时返回的null.为什么会这样?因为调用者并不是当前Http请求,而是一个新的Thread,HttpContext只是与当前Http请求相关(只与当前Http请求对应的线程相关),所以,当利用新的Thread通过HttpContext来访问缓存时,HttpContext.Current因无法从新Thread中获取HttpContext类型实例的上下文信息而返回null,此时,对一个为null的对象进行引用操作时,程序就会报错.
(注:即使把OutHttpContextCacheToString ()方法的错误处理语句去掉,程也不会抛异常,该异常只能在跟踪代码时检查到)
到了这里,我们已经HttpRuntime与HttpContext的区别了.区别就是,HttpRuntime在任何地方都可以使用,而HttpContext则不一定.
尽管结果可能让人有点失望,因为它们只有这么一点区别(我所知道的),但了解这一点是有好处的.最起码你知道了不能脱离当前Http请求而去访问HttpContext.Current属性.
至于Page.Cahce,我认为它应该与HttpRuntime.Cache是相同的.我没有做Demo.写了这么多不想写了,留别人去验证吧!
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/flysun0311/archive/2008/09/03/2876147.aspx
HttpRuntime.Cache
Page.Cache
HttpContext.Current.Cache
我们主要讨论这三种方式到底有什么不同.
首先,在查看Cache的定义时,可以发现,这三个类的Cache是同一个东西.这就表示,不论你使用那种方法在服务器端建立一个缓存,利用其它两种方式都是可以访问到的.实践一下就知道了.
首先,在一页面设置缓存,如下代码
if ( Page.Cache["PageCache"] == null )
{
Page.Cache.Insert("PageCache","This is PageCache") ;
Response.Write( "设置成功!<br>" ) ;
}
if ( HttpContext.Current.Cache["HttpCache"] == null )
{
HttpContext.Current.Cache.Insert("HttpCache","This is HttpCache");
Response.Write( "设置成功!<br>" ) ;
}
if ( HttpRuntime.Cache["HttpRuntimeCache"] == null )
{
HttpRuntime.Cache.Insert("HttpRuntimeCache" , "This is HttpRuntimeCache" ) ;
Response.Write( "设置成功!<br>" ) ;
}
然后,在另一页面通过不同的方式来访问这些已经设置过的缓存.如下代码:
Response.Write( "第一部分:<br>" ) ;
Response.Write( "----利用HttpContext.Current返回的HttpContext访问通过Page.Cache设置的缓存:" ) ;
if ( HttpContext.Current.Cache["PageCache"] != null )
{
Response.Write( "其值为:" +HttpContext.Current.Cache["PageCache"].ToString() +"<br>") ;
}
else
{
Response.Write( "没有访问到!<br>") ;
}
Response.Write( "----利用HttpRuntime.Cach访问通过Page.Cache设置的缓存:" ) ;
if ( HttpRuntime.Cache["PageCache"] != null )
{
Response.Write( "其值为:" +HttpRuntime.Cache["PageCache"].ToString() +"<br>") ;
}
else
{
Response.Write( "没有访问到!<br>") ;
}
Response.Write( "第二部分:<br>" ) ;
Response.Write( "----利用HttpContext.Current返回的HttpContext访问通过HttpRuntime设置的缓存:" ) ;
if ( HttpContext.Current.Cache["HttpRuntimeCache"] != null )
{
Response.Write( "其值为:" +HttpContext.Current.Cache["HttpRuntimeCache"].ToString() +"<br>") ;
}
else
{
Response.Write( "没有访问到!<br>") ;
}
Response.Write( "----利用Page.Cach访问通过HttpRuntime.Cache设置的缓存:" ) ;
if ( Page.Cache["HttpRuntimeCache"] != null )
{
Response.Write( "其值为:" +Page.Cache["HttpRuntimeCache"].ToString() +"<br>") ;
}
else
{
Response.Write( "没有访问到!<br>") ;
}
Response.Write( "第三部分:<br>" ) ;
Response.Write( "----利用HttpRuntime.Cache访问通过HttpContext.Current.Cache设置的缓存:" ) ;
if ( HttpRuntime.Cache["HttpCache"] != null )
{
Response.Write( "其值为:" +HttpRuntime.Cache["HttpCache"].ToString() +"<br>") ;
}
else
{
Response.Write( "没有访问到!<br>") ;
}
Response.Write( "----利用Page.Cach访问通过HttpContext.Current.Cache设置的缓存:" ) ;
if ( Page.Cache["HttpCache"] != null )
{
Response.Write( "其值为:" +Page.Cache["HttpCache"].ToString() +"<br>") ;
}
else
{
Response.Write( "没有访问到!<br>") ;
}
执行的结果与之前的设想是一样的.那么这几种访问缓存的方式到底有什么不一样呢?看下面的代码!
方法一:访问HttpContext.Current.Cache
private void OutHttpContextCacheToString()
{
try
{
if ( HttpContext.Current.Cache["HttpCache"] != null )
{
Response.Write( HttpContext.Current.Cache["HttpCache"].ToString() + "<br>" ) ;
}
else
{
Response.Write( "没有访问到缓存或缓存没有设置!<br>" ) ;
}
}
catch
{
Response.Write( "发生错误!<br>" ) ;
}
}
方法二:访问Httpruntime.Cache
private void OutHttpRumtimeCacheToString()
{
if ( HttpRuntime.Cache["HttpRuntimeCache"] != null )
{
Response.Write( HttpRuntime.Cache["HttpRuntimeCache"].ToString() + "<br>" ) ;
}
else
{
Response.Write( "没有访问到缓存或缓存没有设置!<br>" ) ;
}
}
上面的两个方法是通过两种不同的方式去读取缓存.如果你是在程里直接使用的话,程序是正常的.如果利用线程去调用这两个方法来读取缓存的话,会有什么结果呢?见下的代码:
Response.Write( "利用线程通过HttpContext.Current.Cache来访问缓存中的数据,访问结果为:<br>" ) ;
Thread TestThreadHttpContext = new Thread( new ThreadStart( OutHttpContextCacheToString ) ) ;
TestThreadHttpContext.Priority = ThreadPriority.Highest ;
TestThreadHttpContext.Start() ;
Response.Write( "利用线程通过HttpRuntime.Cache来访问缓存中的数据,访问结果为:<br>" ) ;
Thread TestThreadHttpRuntime = new Thread( new ThreadStart( OutHttpRumtimeCacheToString ) ) ;
TestThreadHttpRuntime.Priority = ThreadPriority.Highest ;
TestThreadHttpRuntime.Start() ;
在查看结果的时候,你会发现,在执行过程中有错误发生了(通过跟踪代码也可以发现错误).错误发生在线程调用OutHttpContextCacheToString()方法中.而通过线程去调OutHttpRumtimeCacheToString()方法时却一切正常.为什么通过线程去调用OutHttpContextCacheToString()方法时会发生错误呢?这其实就是两种HttpRumtime与HttpContext访问缓存的不同之处.
查看微软的MSDN文档,HttpContext的定义为"封装有关个别 HTTP 请求的所有 HTTP 特定的信息",而HttpRuntime的定义为"为当前应用程序提供一组 ASP.NET 运行时服务",换句话说,一个为是与特定Http请求相关联的,一个是与整个应用程序相关联.找到问题的的在了吗?
我们知道,每一个Http请求都会产生一个HttpContext的实例,该实例可以通过HttpContext的Current属性来获取.该实例包含了当前Http请求的相关上下文信息.如果你对程序进行跟踪,你就会发现,当利用新线程访问HttpContext.Current.Cache时,程序报错,错误并不是发生在Cache,而是发生在HttpContext.Current,因为HttpContext.Current这个属性此时返回的null.为什么会这样?因为调用者并不是当前Http请求,而是一个新的Thread,HttpContext只是与当前Http请求相关(只与当前Http请求对应的线程相关),所以,当利用新的Thread通过HttpContext来访问缓存时,HttpContext.Current因无法从新Thread中获取HttpContext类型实例的上下文信息而返回null,此时,对一个为null的对象进行引用操作时,程序就会报错.
(注:即使把OutHttpContextCacheToString ()方法的错误处理语句去掉,程也不会抛异常,该异常只能在跟踪代码时检查到)
到了这里,我们已经HttpRuntime与HttpContext的区别了.区别就是,HttpRuntime在任何地方都可以使用,而HttpContext则不一定.
尽管结果可能让人有点失望,因为它们只有这么一点区别(我所知道的),但了解这一点是有好处的.最起码你知道了不能脱离当前Http请求而去访问HttpContext.Current属性.
至于Page.Cahce,我认为它应该与HttpRuntime.Cache是相同的.我没有做Demo.写了这么多不想写了,留别人去验证吧!
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/flysun0311/archive/2008/09/03/2876147.aspx
相关文章推荐
- 【十五分钟Talkshow】在.NET应用程序中访问Excel的几种方式
- 在.NET应用程序中访问Excel的几种方式
- .net 1.1中的Cache访问方式
- 使用.net 中的动态方法编程备忘录3(DynamicMethod 的最优访问方式)
- .NET中窗体间相互访问的几种方式
- .NET中窗体间相互访问的几种方式
- .net 中访问 excel文件的两种方式
- 通过“改变引用”和“加锁”的方式提高缓存访问并发度
- Jquery跨域访问以及 .net后台对接的方式
- .net中访问oracle数据库的几种方式(转载)
- .net 2005新特性:以Http方式在互联网上访问VSS
- load:加载页面即访问后台方式—例如可做清后台缓存处理
- .net中访问oracle数据库的几种方式
- .NET应用访问数据库之数据库的开销问题 后续篇(一)缓存的使用
- .NET中数据访问方式(一):LINQ
- .net中访问oracle数据库的几种方式
- .NET通过配置文件的方式实现静态js文件跨域访问
- .net 使用管道方式访问mySQL的连接字符串
- .NET中窗体间相互访问的几种方式
- 索引的访问方式:索引查找、索引扫描