How to effectively use cache in. net

Source: Internet
Author: User
Cache is a high-speed cache. I think many people will have the same first impression on him as I do. I feel that it will certainly improve the system performance and speed. Indeed. The original intention of net to launch the cache is indeed this. So how does cache improve system performance and speed? Can I use cache to improve performance in any situation? Is it because the more cache is used, the better? I have some experience in recently developed projects. I hope to discuss it with you as a summary. If there are any mistakes, I hope you can criticize and correct them.

1. How the cache works. L cache is a public memory disk allocated to the server. A public cache can be created on any client browser through the background Code When it is accessed, it targets all users. session is also a piece of memory on the server, but it targets a single user. It is a memory block of the server, that is, each cache occupies server resources once it is created. So from this point, we can say: the more cache, the better. L The cache has a time limit. If the expiration time of the server is exceeded, the cache will be recycled by the server. L c. cache can store any object 2. How to create and destroy the cache. L create a cache in. In the. NET environment, use the cache. insert (string key, object O) method. The key represents the cache ID, and O represents the object stored in the cache. L destroy the cache. Use the method cache. Remove (string key) where key represents the cache ID. l call cache. cache supports packing/unpacking. For example, you can cache a DataSet object DS through. insert ("dscache", DS) is stored in the cache. You can use dataset DS = (Dataset) cache ["dscache"] to access it.

3. When to use cache. cache is generally used for data that is relatively fixed and frequently used. For example, you can store product information in the invoicing system into the cache, and call the cache when the user calls the product information, which greatly reduces the interaction between the user and the database, improves system performance. On the contrary, the cache is not suitable for scenarios with fast data changes and narrow application scope. For example, you can save a specific purchase order to the cache.

4. cache call considerations. The cache has a time limit. When the expiration time set by the server is exceeded, it will be recycled by the server. When the cache is recycled, the corresponding memory block will be cleared. When you access the object through the cache ["cachekey"] Again, the return value is null. Therefore, the following call causes an exception: DataSet DS = (Dataset) cache ["cacheds"]; datarow DR = Ds. table [0]. row [0]; // error. DS is null and Table 0 does not exist. The correct statement should be: DataSet DSIF (Cache ["cacheds"]! = NULL) {DS = (Dataset) cache ["cacheds"];} else {DS = getdsfromdatabase ();}

Datarow DR = Ds. Table [0]. Row [0];

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.