点击这里给我发消息 点击这里给我发消息

ASP.Net使用缓存技术提高效率

添加时间:2010-1-5
    相关阅读: ASP 技术 页面 Access

ASP.Net的缓存技术大大地提高了效率,本人将代码的简单的实现代码贴出:

  一、在页面中显示时,读缓存数据,加载XML数据

public void LoadData()
 {
  DataView Source = (DataView)Cache["MyData"];
  if(Source == null)
  {
  DataSet ds = new DataSet();
  FileStream fs = new FileStream(Server.MapPath("LoginMsg.xml"), FileMode.Open,FileAccess.Read);
  StreamReader reader = new StreamReader(fs);
  ds.ReadXml(reader);
  fs.Close();
  Source = new DataView(ds.Tables[0]);
  Cache.Insert("MyData", Source, new CacheDependency(Server.MapPath("LoginMsg.xml")));
  ErrorMsg.Text = "从文件中读取的数据";
  }
  else
  {
  ErrorMsg.Text = "从缓存中读取的数据";
  }
  MyDataGrid.DataSource = Source;
  MyDataGrid.DataBind();
 }

  二、用XML存储交互数据,将数据放置到缓存中

private void LoginMsg_Click(object sender, System.EventArgs e)
 {
  if(!Page.IsValid)
  {
  ErrorMsg.Text = "必需输入登录信息";
  return;
  }
  DataSet ds = new DataSet();
  FileStream fs = new FileStream(Server.MapPath("LoginMsg.xml"),FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  StreamReader reader = new StreamReader(fs);
  ds.ReadXml(reader);
  fs.Close();
  DataTable dt = ds.Tables[0];
  DataRow newrow = dt.NewRow();
  newrow["custom_email"] = CustomEamil.Text;
  newrow["custom_pass"] = CustomPass.Text;
  newrow["login_time"] = DateTime.Now.ToString();
  fs = new FileStream(Server.MapPath("LoginMsg.xml"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
  TextWriter writer = new StreamWriter(fs);
  writer = TextWriter.Synchronized(writer);
  ds.Tables[0].Rows.Add(newrow);
  ds.WriteXml(writer);
  writer.Close();
  Cache.Remove("MyData");
  LoadData();
  
 }

 

本文作者:
咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册