咖喱碗糕`

`∧ ∧︵
ミ^ō^ミ灬)~ ~我是只可爱的狐狸```
http://freedom2130.cnblogs.com
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

首先是因为晚上我要把我的博客的访问统计加上去的.起初的想法很简单 随便在 Session_Start 里面,自动往数据表里加 1

但考虑到如果访问人数很高的话是不是每次都很频烦的访问(还好我的访问量并不高).

后来想到在Application_Start的时候存数据到 缓存里,Application_End 存进数据表不就得了,.貌似这个想法不错.但有一个问题,就是不可预测的因素,什么服务突然怎么怎么样啦.所以现在我的做法是,把数据 保存在 缓存里 然后利用timer间隔一定时间往数据表里写.

static Timer timer=new Timer(100 * 60*30);

    void Application_Start(object sender, EventArgs e)
    {
        /---省略---/

        string dtc = "1";
        if (dr.Read())
        {
            dtc = dr["vr_count"].ToString();
        }
        dr.Close();

        Application["total"] = dtc;
     
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        timer.Interval = 100 * 60 * 30;
        timer.Enabled = true;
    }

void timer_Elapsed(object sender, ElapsedEventArgs e)
    {

        int total = Application["total"] != null ? Convert.ToInt32(Application["total"].ToString()) : 0;
        if (total != 0)
        {
            /--数据更新--/            
        }
    }

 

void Session_Start(object sender, EventArgs e)
    {
    

        if (Application["total"] == null)
        {
            int total=1;
            Application.Lock();
            Application["total"] = total;
            Application.UnLock();
        }
        else
        {
            int total = Application["total"] != null ? Convert.ToInt32(Application["total"].ToString()) : 0;
            Application.Lock();
            total++;
            Application["total"] = total;
            Application.UnLock();
        }

    }

 

 

很简单,,但效果已经达到了,,不知道这样做有没有什么不足,,望指教...
http://dubox.cn/content-44.aspx