Blogger

投诉/举报!>>

Blog
more...
photo album
more...
video
more...
Home >> 01 Erotic stories>> Regarding ASP.NET page caching
Blogger:aspnet 2023-06-23 00:07:02

Add Favorites

cancel Favorites

Regarding ASP.NET page caching 

1. ASPX Page Caching Using page caching is very simple. Just add the following declaration at the top of the aspx page: `<%@ OutputCache Duration="60" VaryByParam="none" %>` This will cache the entire page content. The ASP.NET code and data source in the page will not be executed during the caching period; instead, the cached page content will be directly output. Page caching applies to all visitors to this page. Therefore, the pressure on the database is the same whether there are 10,000 visitors, one visit, or 1 million visits.
`Duration="60"` means specifying the cache time as 60 seconds. You can set this value according to your needs. After this time, the cache expires, and it will be cached again for 60 seconds after being regenerated, and so on. `VaryByParam="none"` means setting a cache without parameters (caching with parameters will be discussed below). However, for pages with parameters, such as news content pages (e.g., for the ViewNews.aspx page, which won't automatically read `?id=1`), if the above setting is used, the first news item seen will be cached because `?id=2`, `?id=3`, and so on are simply different parameters of the page. To allow different news items to be cached separately, `VaryByParam="id"` can be set to cache different `id` parameters individually. If there are multiple specific cache parameters, separate the parameter names with semicolons, for example, `VaryByParam="id;number"`. If you want different query strings to create different caches, set `VaryByParam="*"`. Generally, setting `"*"` is sufficient. These two parameters are required and cannot be omitted. Another important parameter is `DiskCacheable="true|false"`, which determines whether to cache data on disk. Setting it to `false` will store cached data in memory. It's important to note that if the page data is small, it's acceptable to cache data in memory. However, if the data is large, it's best to store it on disk; otherwise, it will consume a lot of memory and impact server performance. If data is cached on disk, remember to set the `Duration="` value relatively large, such as `Duration="3600"`. Setting it too small will cause the server to write data to disk too frequently, which will degrade performance. If data is cached in memory, `Duration="` should not be set too long. Of course, the optimal duration will depend on your own experimentation.

2, Disable IE caching Suppose there is a page New.aspx. After the client visits it for the first time, a New.aspx file is generated in the Internet Temporary Files folder. Later, if the data in New.aspx is modified and the page is visited again, IE does not update the data in New.aspx; instead, it opens the page from the first visit! IE automatically (by default) calls the New.aspx file in the Internet Temporary Files folder instead of downloading a new New.aspx file. How can we make IE automatically download a new New.aspx file, like reloading the page after clicking the refresh button? The first solution: Client-side settings: Internet Options > General > Temporary Files > Settings > Check every time this page is visited. When setting this up, it's best to delete temporary files simultaneously. This method allows customers to configure their own browsers. If a customer forgets to do so, new pages will never download locally. What will the customer think? ("It must be the program's fault!") Furthermore, when others access your page, their browsers are controlled by them, so this method is generally not suitable for solving this type of problem. The second solution: Let the program automatically download the page! This method actually prevents the page from being saved in the Internet temporary folder; the browser will download the page every time it's accessed. Simply add `Context.Response.Cache.SetCacheability(HttpCacheability.NoCache);` to the `Page_Load` event in the `New.aspx` code. Without this line, a page file will appear in the Internet temporary folder; with it, it will be gone. However, if the aspx file contains image files or JS files, they will still be downloaded to the Internet temporary folder.

3. Disabling Page Caching When Opening an ASPX Page Using the ShowModalDialog() Function in JavaScript Method 1: First, create an HTML page and nest an iframe inside it. The iframe's src attribute is the ASPX page. Then, ShowModalDialog() will call this HTML page instead of the original ASPX page, thus avoiding caching issues. Method 2: In the Page_load() method of the ASPX page, add the line `Response.expires = -1;` to immediately expire the page. This eliminates the need for a nested HTML page.

4.

Data source caching: For example, setting `ObjectDataSource.CacheDuration` (caching time: seconds) and `EnableCaching=true`. This way, the method specified by `SelectMethod` will only be called to execute the database query every specified time interval of `CacheDuration`, and the cached data will be returned directly at other times. A fixed caching time is suitable for frequently accessed pages such as the homepage and article list pages, but not for post viewing pages. Assuming there are 1 million posts, if each post is cached for a fixed time of 1 hour, and 100,000 posts are viewed within that hour, then 100,000 posts would need to be cached, consuming a lot of memory. Even a seemingly minor post like a "grave post" (likely referring to a post only viewed once in a hundred years) might be cached for an hour, further consuming memory. At this point, a "sliding window" strategy can be used. For example, a post can be cached for 10 minutes. If it is accessed within 10 minutes, the cache expiration time is changed to 10 minutes after the access, and so on. This way, frequently accessed posts can be "cached long-term," while infrequently accessed posts won't occupy the cache for extended periods due to occasional access. The configuration method is: Data source: CacheExpirationPolicy="Sliding"

URL 1:http://localhost:909/htmlBlog/62530.html

URL 2:/Blog.aspx?id=62530&aspx=1

Previous Page :

Next Page :

增加   


comment        Open a new window to view comments