zoukankan      html  css  js  c++  java
  • 使用缓存技术提高效率

    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();

      }

  • 相关阅读:
    Android之TabHost使用(引用SDK例子文档)
    Android之在线词典
    校验插入指定结点是否导致编码循环的示例函数
    查表法按日期生成流水号的示例.sql
    备份数据库并提供下载的asp文件
    Metro风格XAML应用程序性能技巧
    导入文本文件时如何指定字段类型.sql
    宝塔形数据的处理.sql
    将某个目录上的Excel表,导入到数据库中.sql
    Using SqlDataReader’s new async methods in .Net 4.5 Beta
  • 原文地址:https://www.cnblogs.com/skylaugh/p/358659.html
Copyright © 2011-2022 走看看