Use Cache Technology to Improve Efficiency

Source: Internet
Author: User

The cache technology of ASP. NET greatly improves the efficiency.CodeThe following is a simple implementation code:

1. read cached data and load XML data when displayed on the page

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 the file ";
}
Else
{
Errormsg. Text = "data read from the cache ";
}
Mydatagrid. datasource = source;
Mydatagrid. databind ();
}

2. Store Interactive Data in XML and store the data in the cache

Private void loginmsg_click (Object sender, system. eventargs E)
{
If (! Page. isvalid)
{
Errormsg. Text = "login information required ";
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.