Designing an efficient cache management Service C #

Source: Internet
Author: User
Tags datetime

All along, I have found that the speed of the program is not ideal. Through the search code, the Discovery program to the database access is very frequent, and the amount of data retrieved is relatively large. In order for the program to run quickly, I want to use the appropriate caching method for the program.

I tried 5 ways to cache data in C #, as follows:

(if there are omissions, errors are welcome to correct and welcome suggestions.) )

1:session method: This method is for each user, if the user is large, it is recommended not to use this method, otherwise the server resources will be greatly depleted.

2:cache Method:

2.1: For each user to access the data is best consistent, otherwise the different key to identify different caches. (The data to be cached is at least according to the user type, if each user can retrieve different data according to the condition, even if do not cause data confusion, estimate also with the session no different, the user many words too consume server resources).

2.2: If each user gets different data, and the number of users, should be considered to the client to save.

2.3: You need to consider when generating the cache, if the data in the database has changed. The problem is that users are not getting the latest data. (Refer to: "Database Cache dependencies" article)

3: Write files to Client

First a page in the runtime, from the database to the dataset, in the display of data reports, a page of the dataset into an XML file, written to the client. If the customer wants to print the report, the program takes the XML file from the client and, if the XML file exists, reads the data from it and displays 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> //给客户机上写文件
function ws()
{
var fso,ctf;
fso = new ActiveXObject("Scripting.FileSystemObject"); //IE的安全设置里面需要把没有标记为安全的ActiveX控件和脚本 设置
为“启用”,才不会提示。
ctf = fso.CreateTextFile("c:\\luiTestfile.txt",true);//true为可以覆盖原有文件
ctf.Write("write content in file");
ctf.Close();
}
</script>

However, each time you manipulate the client's files, the system prompts you to run an ActiveX control that does not have an identity, and the user point does not save the file successfully.

4: Using Cookies to store

--------------------Write Cookies----------------------------------------
HttpCookie MyCookie = new HttpCookie ("Datecookie_lui");//datecookie_lui the key name of the cookie key value pair to be created.
DateTime now = DateTime.Now;
Mycookie.value = Now. ToString ();//Assign value to this cookie
Mycookie.expires = Now. AddMinutes (1)//Set the expiration date and time of the cookie (this is 1 minutes after expiration), when the customer again access the server with a cookie file, such as the name of the cookie expired, it will automatically clear it.
This. Response.setcookie (MyCookie);//write this cookie to the client
--------------------Read Cookies----------------------------------------
HttpCookie MyCookie = new HttpCookie ("Mytestcookie_lui");//Create a Cookie Action object
MyCookie = request.cookies["Datecookie_lui"];//get cookie data from the client, Datecookie_lui the key name of the cookie key value pair to be fetched
if (MyCookie!= null)
Response.Write ("Cookie Name is:" +mycookie.name+.) Cookie Value is: "+mycookie.value";
Else
Response.Write ("not found!");
Storing data in a cookie method, after my repeated attempts to find that the size of the cookie store value is limited, (only 1686 characters per item, only 5,059 letters or digits), and the size of the cookie file if it exceeds the size (around 20kb) can cause the page to not be found error. And only 20 key-value pairs can be stored in each cookie file.
5: Try to convert the dataset to an XML string in page A, hide it in the page, post it to page B, take the XML string to the B page, and then convert the XML string into a dataset to generate the report.
A string that converts a dataset to XML:
Vdataxmlstr=ds. GETXML ()//To get the dataset's XML string
this.hidxmlstr.value=vdataxmlstr;//in hidden elements of a page
------------------------------------------------------------------------
To convert an XML string into a dataset:
Vxmlstr = request.form["Hidxmlstr"]. ToString ();//The value of the variable that takes an XML string
System.IO.StringReader VSR = new System.IO.StringReader (VXMLSTR);//Read string
Ds. READXML (VSR)//Read XML into the dataset
------------------------------------------------------------
If the font is garbled in the page: to set the page's <%@ pages responseencoding= "gb2312"%>

Reference: Designing an efficient cache management Service

Related Article

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.