C # Cache when to use and how to use

Source: Internet
Author: User

The cache is cached. So how does the cache improve system performance and run speed? Is it possible to use the cache to improve performance under any circumstances? Is it better to use the more cache? I have experience in the recent development of projects, write down as a summary also hope to be able to discuss with you, there are mistakes in the place I hope you criticize.
How does 1.Cache work?
The Cache is a common memory slice that is allocated on the server.
The so-called public refers to the cache as long as a creation is any client browser can be accessed through the background code to it, it is for all users, relatively speaking session is a memory on the server, but he is facing a single user. It is a block of memory for the server, which means that each cache consumes server resources once it is created. So from this we can say: not the more cache the better.
The cache is time-limited, exceeding the server's set expiration time and it will be recycled by the server.
Cache can hold any object
  
2.Cache How to create and how to destroy
Create cache
Under the dotnet environmentCache.Insert (String key,object o)Method is created.
Where key means the id,o of the cache represents the object stored in the cache.
  
Destroy Cache
by methodcache.remove (string key)
Where key represents the ID of the cache.
  
Call the Cache
The cache supports boxing/unpacking operations. If you can put a DataSet object DS throughCache.Insert ("Dscache", DS)stored in the cache and can be removed by unpacking theDataSet ds = (DataSet) cache["Dscache"]To access it.
  
3. When to use the cache
Caches are typically used in more frequent places where data is more fixed. For example, the invoicing system can be stored in the product information cache, the user calls the product information by the cache can be called, which greatly reduces the user's interaction with the database, improve the performance of the system. On the other hand, the cache is not suitable for use in areas where data changes quickly and in a narrow range. For example, a specific purchase order is credited to the cache.
  
4.cache Call Considerations
There is a time limit for the cache. When the server setting expires, it is recycled by the server. When the cache is reclaimed, the corresponding memory block is emptied, and the null value is returned when the object is accessed again by cache["CacheKey". So the following call will be an exception
  
  

DataSet ds == ds. table[0]. row[0]; // Error, DS is a null value, table 0 does not exist. 

  
The correct wording should be:
  

   DataSet ds;     ifnull) {    = (DataSet) cache["Cacheds"]; }  Else  {    ds== ds. table[0]. row[0];    

C # Cache when to use and how to use

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.