C # Summary of data cache experience in programming

Source: Internet
Author: User

I have found that Program The running speed is not ideal. Query Code It is found that the program frequently accesses the database and the retrieved data volume is large. To make the program run faster, I want to use the appropriate caching method for the program.
I tried five data caching methods in C:
(If any omission exists, you are welcome to correct the error and give suggestions .)
1: Session method: This method is intended for each user. If the number of users is large, we recommend that you do not use this method. Otherwise, server resources will be exhausted.
2: cache method:
2.1: The data accessed by each user should be consistent. Otherwise, different keys should be used to identify different caches. (The data to be cached is at least divided by user type. If each user can retrieve different data based on conditions, even if data is not disordered, it is estimated that there is no difference with session. If there are many users, it will consume too much server resources ).
2.2: if each user has different data and a large number of users, you should consider storing the data on the client.
2.3: After the cache is generated, you need to consider if the data in the database changes. This causes users to fail to obtain the latest data. (See "database cache Relevance" Article )
3: write files to the client
First, when page a is running, the dataset is obtained from the database. When the data report is displayed, page a converts the dataset into an XML file and writes it to the client. If the customer wants to print the report, the program will fetch the saved XML file from the client. If the XML file exists, the program will read the data and display it as a printed report. If the XML file does not exist, you are prompted to retrieve the data again.
For example:
<SCRIPT type = text/JavaScript> // write a file to the client
Function WS ()
{
VaR FSO, CTF;
FSO = new activexobject ("scripting. FileSystemObject"); // you must set ActiveX controls and scripts that are not marked as secure in the security settings of IE.
If it is enabled, no prompt is displayed.
CTF = FSO. createtextfile ("C: \ luitestfile.txt", true); // true indicates that the original file can be overwritten.
CTF. Write ("write content in file ");
CTF. Close ();
}
</SCRIPT>
However, each time a client file is operated, the system prompts whether to run the unidentified ActiveX control. If you click "no", the file cannot be saved.
4: Use cookies for storage
-------------------- Write cookie ----------------------------------------
Httpcookie mycookie = new httpcookie ("datecookie_lui"); // datecookie_lui is the key name of the cookie key-value pair to be created.
Datetime now = datetime. now;
Mycookie. value = now. tostring (); // assign a value to this cookie
Mycookie. expires = now. addminutes (1); // set the cookie expiration date and time (1 minute later). When the customer accesses the server again, the cookie file is included, if the cookie with this name expires, it is automatically cleared.
This. response. setcookie (mycookie); // write this cookie to the client
------------------ Read cookie ----------------------------------------
Httpcookie mycookie = new httpcookie ("mytestcookie_lui"); // create a cookie operation object
Mycookie = request. Cookies ["datecookie_lui"]; // obtain the cookie data sent from the client. datecookie_lui is the key name of the cookie key-value pair to be retrieved.
If (mycookie! = NULL)
Response. Write ("Cookie name is:" + mycookie. Name + ". Cookie value is:" + mycookie. value );
Else
Response. Write ("not found! ");
The cookie method is used to store data. after repeated attempts, I found that the cookie storage value is limited in size (each item can only store 1686 Chinese characters, and only 5059 letters or numbers ), if the size of the cookie file exceeds the size (about 20 KB), the page cannot be found, and only 20 key-value pairs can be saved in each cookie file.
5. Try to convert dataset to an XML string on page A, hide it in the page, and submit post to page B. Obtain the XML string on page B and convert the XML string to dataset, generate a report.
String for converting dataset into XML:
Vdataxmlstr = Ds. getxml (); // obtain the XML string of the dataset.
This. hidxmlstr. value = vdataxmlstr; // save it to the hidden element of the page.
------------------------------------------------------------------------
Convert an XML string to Dataset:
Vxmlstr = request. Form ["hidxmlstr"]. tostring (); // retrieve the variable value of the XML string
System. Io. stringreader VSR = new system. Io. stringreader (vxmlstr); // read the string
DS. readxml (VSR); // read XML into Dataset
------------------------------------------------------------
If the font is garbled: <% @ page responseencoding = "gb2312" %>

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.