If you query the database every time you enter the page to generate the page content, if the traffic is very large, the site performance will be very poor. And if only when the first visit to query the database generated page content, and then directly output content, you can improve system performance. This way, no matter how many people access the database only once, the database pressure is constant.
Caching (cache) is a space-for-time technology that exists in many parts of a computer and is used to store frequently used data from slow devices in a fast device, taking data directly from a fast device. such as CPU level two cache, memory, Windows file read cache.
The problem with cache invalidation: In order to ensure that the data in the cache is read and slow data (database) is consistent, it is necessary to clear the corresponding data in the cache when the corresponding data in the slow data (database) is changed.
Caching is the first means of improving the performance of a website, just as the index is the first means of improving the performance of a database. The ASP. NET cache is divided into three main types: page caching (moderation), data source caching (least flexible), and data caching (flexibility).
1. Caching can improve data access performance because the cached data is in memory. Avoids disk I/O operations, or network connections to the database.
2. Cached data, must have a certain expiration policy, otherwise the actual data changes, the corresponding cache or old data, resulting in inconsistent data issues.
3. What is the use of caching?
The data accessed by 1> is not changed or is rarely changed.
2> data is frequently accessed.
Use the cache directly
1> uses the cache["content" directly, the cache is different from the session and can be shared by all users. Never expires, maintained by the server itself, and when memory is insufficient, the old cache is freed.
2> set an absolute expiration date. Cache.Insert ("Nowtime", DateTime.Now, NULL, DateTime.Now.AddSeconds (7), TimeSpan.Zero);
3> Set sliding expiration Date: Cache.Insert ("Nowtime", DateTime.Now, NULL, DateTime.MaxValue, Timespan.fromseconds (5));
if (cache["nowtime"] = = null)
{
Cache.Insert ("Nowtime", DateTime.Now, NULL, DateTime.MaxValue, Timespan.fromseconds (5));
}
Else
{
Response.Write ("Time in cache:" + cache["nowtime"]);
}
----------------------------------------------------------
if (cache["time"] = = null)
{
cache["Time"] = DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss");
}
Response.Write (cache["Time"]);
----------------------------------------------------------
if (cache["time"] = = null)
{
Cache.Insert ("Time", DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss"), NULL, DateTime.Now.AddSeconds (TEN), TimeSpan.Zero );
}
Response.Write (cache["Time"]);
----------------------------------------------------------
if (cache["time"] = = null)
{
Cache.Insert ("Time", DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss"), NULL, DateTime.MaxValue, Timespan.fromseconds (5) );
}
Response.Write (cache["Time"]);
----------------------------------------------------------
Page Caching
025-Caching cache