This article mainly introduces the cache used in ASP.net, this article explains how the cache is working, how to create and how to destroy cache, when the cache, cache call attention, and other content, the need for friends can refer to the
Cache is caching, I think very many people to his first impression must be the same as me, I feel he will improve the system performance and speed of operation. Do. NET to launch the cache is really the original intention is this. So how does cache improve the performance and speed of the system? Is it possible to improve performance with cache in all cases? is the cache used more than the better? I have experience in recent research and development projects, write down as a summary also hope to discuss with you, Where there is a mistake, I hope you will criticize me.
1.Cache is how to work
The Cache is a public memory slice assigned to the server.
The so-called public refers to the cache as long as the creation is all a client browser can be accessed through the background code to him, he is oriented to all users, relatively session is also a piece of memory on the server, but he is oriented to a single user. He is a block of memory in the server, which means that every cache is created and consumes server resources. So from this point we can say: not the more the cache the better.
Cache has a time limit, exceeding the expiration time set by the server, and he will be reclaimed by the server.
C.cache can store all objects
2.Cache How to create and how to destroy
Create cache
In. NET environment, created by the Cache.Insert (String Key,object o) method. Where key represents the cache of the Id,o representative to save the object in the cache.
Destroy the cache.
by method Cache.remove (string key), where key represents the cache ID.
Call Cache.
The cache supports boxing/unboxing operations. If you can save a DataSet object DS to the Cache via Cache.Insert ("Dscache", DS), you can access the DataSet ds = (DataSet) cache["Dscache" through the unboxing operation.
3. When to use cache
Cache is generally used for data more fixed, used more frequently in the place. For example, invoicing system can be stored in the product information cache, when the user calls the product information through the call cache can be, so that greatly reduce the user and database interaction, improve the performance of the system. Conversely, the cache is not suitable for fast data changes, the use of a very narrow range of places. For example, a specific purchase order is deposited in the cache.
4.cache Call Considerations
There is a time limit to the cache. exceeds the expiration time of the server setting, it is reclaimed by the server. When the cache is reclaimed, the corresponding block of memory is emptied and the null value is returned when the object is accessed again via cache["CacheKey". So the following call will have an exception
?
1 2 3 |
DataSet ds = (DataSet) cache["Cacheds"]; DataRow dr = ds. Table[0]. ROW[0]; Error, DS is null value, no table 0 exists. |
The correct wording should be:
?
1 2 3 4 5 6 7 8 9 |
DataSet DS If (cache["cacheds"]!= null) {ds = (DataSet) cache["Cacheds" ]; Else {ds= getdsfromdatabase (); DataRow dr = ds. Table[0]. ROW[0]; |