.NET环境下使用Session、ViewState、Cache等

首先请看如下的代码:
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.Caching;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class _Default : System.Web.UI.Page 
  12. {
  13.     private static int counter = 0;
  14.     private static object lockObject = new object();
  15.     private static object catheSynchronize = new object();
  16.     protected void Page_Load(object sender, EventArgs e)
  17.     {
  18.         ViewState["NUM"] = 4;
  19.     }
  20.     protected void Button1_Click(object sender, EventArgs e)
  21.     {
  22.         //1
  23.         Application.Lock();
  24.         if (Application["HitCounter"] == null)
  25.         {
  26.             Application["HitCounter"] = 0;
  27.         }
  28.         Application["HitCounter"] = (int)Application["HitCounter"] + 1;
  29.         Application.UnLock();
  30.         //2
  31.         lock (lockObject)
  32.         {
  33.             counter++;
  34.         }
  35.         //3
  36.         Session["NUM"] = 10000;
  37.         if (Cache["NUM_CACHE"] == null)
  38.         {
  39.             lock (catheSynchronize)
  40.             {
  41.                 if (Cache["NUM_CACHE"] == null) {
  42.                     //Cache["NUM_CACHE"] = 100; 
  43.                     CacheDependency cd = new CacheDependency(Server.MapPath("aaaa.xml"));
  44.                     Cache.Insert("NUM_CACHE""OOOOOK", cd, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration);
  45.                 }
  46.             }
  47.         }
  48.         //Cache.Remove("NUM_CACHE");
  49.         Response.Redirect(string.Format("TestPage.aspx?type={0}", counter));
  50.     }
  51. }
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class TestPage : System.Web.UI.Page
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         //Label1.Text = Convert.ToString(Session["NUM"]);
  16.         //Label1.Text = Convert.ToString(ViewState["NUM"]);
  17.         Label1.Text = Convert.ToString(Cache["NUM_CACHE"]);
  18.         //Label1.Text = Convert.ToString(Request.QueryString["type"]);
  19.     }
  20. }
以上演示了 Session, ViewState, Cache的基本操作,以及 Application的用法。
其中, Application因为涉及到并发问题,在操作 Application的事后一般会采取锁机制。这样就可以使用 Application来实现计数器等的功能了。
当然,我们也可以使用 lock 模块来实现这种锁机制。

关于缓存的说明,请参照:
http://www.cnblogs.com/shimeng3344518/archive/2007/05/15/747606.html
http://stor.chinaunix.net/stor/m/2008/07/15/1200353.shtml
http://www.qqread.com/aspdotnet/p392191.html
http://www.dreamsun.cn/web/show.asp?id=1513
http://www.opent.cn/?action=show&id=203
http://www.nuli365.com/ReadCode.aspx?id=401
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值