缓存的运用

using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //using System.Diagnostics
        Stopwatch sw = new Stopwatch();
        sw.Start();
        for (int i = 0; i < 100; i++)
        {
            Response.Write(i.ToString()+"<br/>");
        }
        Response.Write("--------------------------------------------");
        Response.Write(sw.ElapsedTicks);
    }

    public DataSet Dbind()
    {
        DataSet ds = new DataSet();
        using (SqlConnection con = new SqlConnection("server=.;database=a;uid=sa;pwd=123456"))
        {
            con.Open();
            using (SqlDataAdapter cmd = new SqlDataAdapter("select * from a", con))
            {
                cmd.Fill(ds);
            }
        }
        return ds;
    }

    //从数据库读出来
    protected void Button3_Click(object sender, EventArgs e)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        DataSet ds= Dbind();
        Response.Write(string.Format("查询结果:{0}<br/>", ds.Tables[0].Rows[300000][0]));
        Response.Write(string.Format("耗费时间:{0}<br/>", sw.ElapsedTicks));   //时间 10978159

    }

    //从缓存读出来
    protected void Button4_Click(object sender, EventArgs e)
    {
        DataSet ds;
        Stopwatch sw = new Stopwatch();
        sw.Start();
        if (Cache["Data"] != null)
        {
            ds = Cache["Data"] as DataSet;
        }
        else
        {
            ds = Dbind();
        }
        Response.Write(string.Format("查询结果:{0}<br/>", ds.Tables[0].Rows[300000][0])); 
        Response.Write(string.Format("耗费时间:{0}<br/>", sw.ElapsedTicks));   //时间 60
    }

    //写入缓存
    protected void Button2_Click(object sender, EventArgs e)
    {
        DataSet ds = Dbind();
        Cache.Insert("Data", ds);  
    }

    //删除缓存
    protected void Button5_Click(object sender, EventArgs e)
    {
        Cache.Remove("Data");
    }
}

 

http://www.cnblogs.com/luluping/archive/2009/09/13/1565732.html

posted @ 2011-12-02 14:19  踏浪帅  阅读(187)  评论(0编辑  收藏  举报