ASP.NET 应用程序缓存的研究;首先新建一个 Web 窗体,默认状况下就可以直接使用 Cache 对象来进行缓存的管理,但很是奇怪的是在 Visual Studio 中,当鼠标放到这个 Cache 上时会出现来自 System.Web.Caching.Cache 的提示,但实际上你不能直接使用这个命名空间加上类型来管理缓存,不然会出现错误;当本身键入这个命名空间加上 Cache 时只会出现两个名字带 Expiration 的成员。来自两个不一样命名空间的 Cache 对象管理缓存实际上效果是同样的,它们可能都直接做用于当前 Web 应用程序的缓存,以下代码:缓存
System.Web.HttpRuntime.Cache.Insert("cache_test", "System.Web.HttpRuntime.Cache success.<br/>", null, DateTime.Now.AddSeconds(5), System.Web.Caching.Cache.NoSlidingExpiration); System.Web.Caching.Cache cache = new System.Web.Caching.Cache(); Response.Write(System.Web.HttpRuntime.Cache.Get("cache_test").ToString()); Response.Write(Page.Cache.Get("cache_test").ToString()); Response.Write(this.Cache.Get("cache_test").ToString()); Response.Write(cache.Get("cache_test").ToString()); cache.Insert("cache_test", "System.Web.Caching.Cache success.<br/>", null, DateTime.Now.AddSeconds(5), System.Web.Caching.Cache.NoSlidingExpiration); Response.Write(System.Web.HttpRuntime.Cache.Get("cache_test").ToString()); Response.Write(Page.Cache.Get("cache_test").ToString()); Response.Write(this.Cache.Get("cache_test").ToString()); Response.Write(cache.Get("cache_test").ToString()); //对象引用对于非静态的字段、方法或属性“Cache.Insert(...)”是必需的 //System.Web.Caching.Cache.Insert("cache_test", "System.Web.Caching.Cache success.", null, DateTime.Now.AddSeconds(5), System.Web.Caching.Cache.NoSlidingExpiration); //对象引用对于非静态的、方法或属性“Cache.Get(...)”是必需的 //Response.Write(System.Web.Caching.Cache.Get("cache_test").ToString());
由于建立的 Web 窗体会默认继承自 System.Web.UI.Page,因此可以直接使用 Page 类提供的公开成员 Cache;System.Web.HttpRuntime.Cache 是静态类,也可以直接使用;就只有 System.Web.Caching.Cache 须要实例化后使用。 最终的输出结果以下:this
System.Web.HttpRuntime.Cache success.
System.Web.HttpRuntime.Cache success.
System.Web.HttpRuntime.Cache success.
System.Web.HttpRuntime.Cache success.
System.Web.Caching.Cache success.
System.Web.Caching.Cache success.
System.Web.Caching.Cache success.
System.Web.Caching.Cache success.code
相关环境:
.NET Framework 4.0对象