Introduction to the use of Cache in ASP.NET

  • 2021-06-28 12:19:48
  • OfStack

Cache is a cache. I think many people will think that his first impression 1 is the same as mine. I think Cache can improve the performance and speed of the system.You bet.Net launched cache in the very beginning.So how does cache improve system performance and speed?Does cache improve performance in all cases?Is the more cache you use, the better?I have experience in recent research and development projects. Write it down as a summary and hope to discuss it with you. If there are errors, I hope you will criticize and correct them.
*
1. How does Cache work
*
Cache is a common memory slice allocated on the server.
*
The so-called public cache only needs 1 to be created so that all client browsers can access it through background code. It is for all users. session is also a segment of memory on the server, but it is for a single user.He is a memory block of the server, that is, each cache1 is created and consumes server resources.So from this point we can say: not that more cache is better.
*
cache has a time limit and will be recycled by the server if it exceeds the expiration time set by the server.
*
c.cache can store all objects
*
2. How Cache was created and destroyed
*
Create cache
*
In.The Net environment is created by the Cache.Insert (string key, object o) method.key represents ID of cache and o represents the object stored in cache.
*
Destroy cache.
*
Through the method Cache.Remove (string key), where key represents ID of cache.
*
Call cache.
*
Cache supports boxing/unboxing operations.If you can store an DataSet object, ds, in Cache by Cache.Insert ("dsCache", ds), you can access it by unpacking DataSet ds = (DataSet) Cache ["dsCache"].
*
3. When to use cache
*
Cache 1 is generally used in places where data is more fixed and frequently used.For example, the product information can be stored in cache in the sales and purchase system, and cache can be invoked when the user invokes the product information, which greatly reduces the interaction between the user and the database, and improves the performance of the system.Conversely, cache is not suitable for places where data changes rapidly and the range of use is very narrow.For example, store a specific purchase order in cache.
*
4.cache Call Notes
*
Cache has a time limit.If the expiration time set by the server is exceeded, it will be recycled by the server.When cache is recycled, the corresponding memory block is emptied and the null value is returned when the object is accessed again through cache ["cachekey"].So the following call would be an exception
*


 * DataSet ds = (DataSet)Cache[ " cacheds " ];
 * 
 * DataRow dr = ds.Table[0].Row[0];   // Error, ds by null Value, no table exists 0 . 


*
The correct writing should be:
*
*


DataSet ds
 * 
 * If(Cache[ " cacheds " ] != null)
 * 
 * {
 * 
 * ds = (DataSet)Cache[ " cacheds " ];
 * 
 * }
 * 
 * Else
 * 
 * {
 * 
 * ds= GetDsFromDataBase();
 * 
 * }
 * 
 * DataRow dr = ds.Table[0].Row[0];


Related articles: