Asp. NET uses caching technology to improve efficiency

Source: Internet
Author: User
Asp.net| Cache ASP. NET cache technology has greatly improved the efficiency, I will code the simple implementation of the code posted:

When the page is displayed, read the cached data, load the XML data

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 = "Data read from a file";
}
Else
{
Errormsg.text = "Data read from cache";
}
Mydatagrid.datasource = Source;
Mydatagrid.databind ();
}

Second, using XML to store interactive data and put data into the cache

private void Loginmsg_click (object sender, System.EventArgs e)
{
if (! Page.IsValid)
{
Errormsg.text = "must enter login information";
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 ();
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.