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 backgroundCodeWhen 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 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 cache.
In the cache. Remove (string key) method, the 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 when data 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, 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];