How to effectively use cache in. net

Source: Internet
Author: User
Cache is a high-speed cache. I think many people have the same first impression on him. 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 all cases? Is it because the more cache is used, the better? I have some experience in recent R & D projects, and 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 does cache work?
  
Cache is a public memory disk allocated to the server.
  
The so-called public cache allows all client browsers to access the cache through background code. It targets all users, and session is a memory segment on the server, however, 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.
  
The cache has a time limit. When the expiration time of the server is exceeded, the cache will be recycled by the server.
  
C. cache can store all objects
  
   2. How to create and destroy a cache
  
Create 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.
  
Destroy cache.
  
In the cache. Remove (string key) method, the key represents the cache ID.
  
Call cache.
  
Cache supports packing/unpacking. If you can pass a DataSet object ds to the cache. insert ("dscache", DS) is stored in the cache, and can be accessed by unpacking dataset DS = (Dataset) cache ["dscache.
  
   3. When to use cache?
  
Cache is generally used when data is relatively fixed and frequently used. For example, you can store product information into the cache in the invoicing system and call the cache when you call product information. This greatly reduces the interaction between users and databases, improves system performance. On the contrary, the cache is not suitable for areas 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, exceptions may occur in the following calls:
  
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 DS
  
If (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.