乐趣区

关于asp.net:ASPNET-应用程序缓存

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 须要实例化后应用。最终的输入后果如下:

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.

相干环境:
.NET Framework 4.0

退出移动版