In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to cache asp.net, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
First, the concept of cache, the benefits and types of cache.
Caching is a technique that trades space for time, which means to store the data you get in memory for a period of time. In this short period of time, the server does not read the database or the real data source, but reads the data you store in memory. Here you will wonder how to set up the storage data, what kind of data can be stored, and the setting of storage time. There is a deviation when the real data source data is changed by the server. Don't worry, we'll talk about it later.
The advantage of caching, caching is an indispensable data processing mechanism for website performance optimization. It can effectively relieve the pressure on the database. For example, the website has a click rate of 1 million per minute. If you do not use cached static pages, there is no viewstate (viewstate will generate a large number of strings, which is a pressure on the server to interact with data, so the general page is to disable viewstate, using caching). The user can only click on the page once, and the page can read the database once, so the pressure on the database can be imagined. If we use the cache here and set the cache validity period to 1 minute, then this minute is only within one minute. 1 million clicks are the same as one click. The database is read once, and the data source is cached in memory.
Caching in asp.net can be divided into three main types: page cache, data source cache and custom data cache.
II. Data caching
The copy code is as follows:
Public partial class WebForm1: System.Web.UI.Page
{
Protected void Page_Load (object sender, EventArgs e)
{
/ / Cache ["date"] = data to be cached; here is a simple declaration of custom caching using the
String datastr = DateTime.Now.ToLongTimeString ()
Response.Write ("first output time:" + datastr+ ""); / / the current time read here, the time here will change when the page is refreshed.
If (Cache ["date"] = = null) / / determine whether there is a cache with a value of date.
{
Cache ["date"] = datastr
Response.Write ("the second output time is:" + Cache ["date"] + "the current time read here"); / / the current time read here, the time here will change when the page is refreshed.
}
Else
{
Response.Write (Cache ["date"] + "here is the time to read from the cache"); / / the time in the cache read here will not change over time when the page is refreshed.
}
}
}
The above data cache does not set the cache expiration time, so the first output time is the current time (the refresh page will change), and the second output time will always be the first time stored in the cache (the refresh page will remain the same).
Let's add some useful parameters to the data cache (code above).
The copy code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
String ids= ""
Maticsoft.BLL.ScriptsBak bll = new Maticsoft.BLL.ScriptsBak ()
List list = new List ()
List = bll.GetAll ()
For (int I = 0; I < list.Count; iTunes +)
{
Ids + = list [I] .ScriptId.ToString () + "-"
}
Ids = ids + "finish"; / / the ids here is a string that reads the id value in the table from the database and then links it with--
If (Cache ["key"] = = null)
{
Cache.Insert ("key", ids, null, DateTime.Now.AddSeconds (40), System.Web.Caching.Cache.NoSlidingExpiration); / / the data is cached here and the cache time is set
/ / "key" is the cached key, ids is the cached value, and null is the cache dependency. No cache dependency is used here, so it is null. The cache dependency will be described in more detail below.
/ / the cache time after null is 40 seconds.
/ / the last parameter is the format of setting time. ASP.NET allows you to set an absolute expiration time or a sliding expiration time, but not at the same time.
/ / the absolute expiration time is set here, that is, the cached data is 40 seconds after the page is not refreshed, and it will be retrieved from the database after 40 seconds.
Response.Write ("cache loaded as--" + Cache ["key"] + "")
}
Else
{
Response.Write ("cache loaded as--" + Cache ["key"] + "")
}
Response.Write ("directly loaded as -" + ids + "")
}
Data cache: add some time-consuming entries to an object cache collection and store them as keys. We can set cache expiration, priority, dependencies, etc., by using the Cache.Insert () method.
Page caching
The copy code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
String date = DateTime.Now.ToString ()
Response.Write (date)
}
The copy code is as follows:
This instruction tag adds cache to the page. The parameter Duration specifies that the page cache time is 10 seconds. VaryByParam, the specified page parameter, is like this. If this is the page, we can write the instruction tag as the parameter and the parameter are separated by a semicolon. In this way, each individual page is cached. It caches all the pages with different parameters, such as postid=2536603&update=1 or postid=1&update=2. An easy way to use this is to cache all pages with different parameters under the current page.
ASP.NET will no longer execute the page's life cycle and related code, but will directly use the cached page, which is simply explained in my comments.
Fourth, control cache
For data source controls such as 1.ObjectDataSource, you can find the corresponding properties in the property bar and set them. I'll list an example below. Set the cache to 10 seconds and the time type to absolute time.
two。 Controls without cache properties need to be cached
The copy code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
String date = DateTime.Now.ToString ()
TextBox1.Text = date
}
The copy code is as follows:
The TextBox control here is cached, and the cache time here is 10 seconds, that is, within 10 seconds, ASP.NET will no longer execute the page's life cycle and related code, but will directly use the cached page.
Fifth, cache dependence
The copy code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
String str = ""
If (Cache ["key"] = = null)
{
Str = System.IO.File.ReadAllText (Server.MapPath ("TextFile1.txt")); / / read data in TextFile1.txt file
CacheDependency dp = new CacheDependency (Server.MapPath ("TextFile1.txt")); / / establish cache dependency dp
Cache.Insert ("key", str, dp)
Response.Write (Cache ["key"]); / / if the contents of the TextFile1.txt file remain unchanged, the data in the cache will be read all the time, and once the data in the TextFile1.txt file changes, the data in the TextFile1.txt file will be re-read.
}
Else
{
Response.Write (Cache ["key"])
}
}
Cache dependencies make the cache dependent on other resources, and cache entries are automatically removed from the cache when the dependency changes. Cache dependencies can be files, directories, or keys with other objects in the application's Cache. If the file or directory changes, the cache expires.
6. Set cache in configuration file
The copy code is as follows:
The copy code is as follows:
This adds a page with a cache of 60 seconds.
7. Callback function of cache
The copy code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
String str = ""
If (Cache ["key"] = = null)
{
Str = System.IO.File.ReadAllText (Server.MapPath ("TextFile1.txt")); / / read data in TextFile1.txt file
CacheDependency dp = new CacheDependency (Server.MapPath ("TextFile1.txt")); / / establish cache dependency dp
Cache.Insert ("key", str, dp, DateTime.Now.AddSeconds (20), Cache.NoSlidingExpiration, CacheItemPriority.Low, CacheItemRemovedCallback)
The parameter / / CacheItemPriority is the priority of the cache, which is divided into many levels. In order to prevent the system from deleting the cache when the cache is full, it is followed by the name of the callback function.
Response.Write (Cache ["key"]); / / if the contents of the TextFile1.txt file remain unchanged, the data in the cache will be read all the time, and once the data in the TextFile1.txt file changes, the data in the TextFile1.txt file will be re-read.
}
Else
{
Response.Write (Cache ["key"])
}
}
Public void CacheItemRemovedCallback (string key, object value, CacheItemRemovedReason reason) / / this is the callback function when the cache is removed. It must be consistent with the name of the last parameter in the Cache.Insert () method.
/ / delegation is used here, and you can transfer what you see in the definition in the function Cache.Insert (), so the format here can only be signed as I wrote.
{
System.IO.File.WriteAllText (Server.MapPath ("log.txt"), "the reason for cache removal is" + reason.ToString ()
}
The callback function in the example is about generating a log.txt that documents the reason for each cache removal.
VIII. Cache settings in the configuration file
Our server has the caching function, which can reduce the compilation time of the website in the server when you visit the website, and greatly speed up the access speed of your website. If you need to update your website frequently, you can consider reducing the caching time temporarily or turning off the cache temporarily.
Please put the following code in the web.config file in the root directory of your website
1. To set the time to shrink the cache in web.config, please use the following definition in web.config
two。 If you want to turn off the caching function of a page, use the following definition in web.config
3. If you want to turn off the caching function of the entire program, use the following definition in web.config
4. If you want to turn off the caching function of one or more folders in the root directory, use the following definition in web.config
The above is all the contents of the article "how to cache asp.net". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.