当前位置:编程学习 > asp >>

asp.net cache实用

  if (!IsPostBack)//第一次加载
            {
                Stopwatch watch = new Stopwatch();//定义缓存
                watch.Start();
                if (Cache["grda"] == null) //如果cache为空
                {
                    GRDA grda = new GRDA();
                    DataTable dt = grda.GetGrda();  //获取数据库信息

                    //为缓存赋值的最简单的方式
                    Cache["grda"] = dt;

                    //可以设置过期时间的缓存赋值方式
                    Cache.Insert("grda", dt, null, DateTime.Now.AddSeconds(30), TimeSpan.Zero);
                    //创建关联文件
                    CacheDependency file = new CacheDependency("c:\\11.txt");

                    Cache.Insert("grda", dt, file, DateTime.Now.AddMinutes(30), TimeSpan.Zero);

//(cache名,数据,关联文件,session时间,点击增加十分钟有效期(一般不用))

                }

                this.GridView1.DataSource = (DataTable)Cache["grda"];//从缓存中取出数据
                this.GridView1.DataBind();
                watch.Stop();//停止缓存
                double totaltime = watch.Elapsed.TotalSeconds;
                this.Label1.Text = totaltime.ToString();//显示失效时间

 

//以上分析属于个人观点   如有不同请加Q472613400  讨论



摘自 liuningshiwoa的专栏
补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,