1. Cache Mechanism
Output Cache
The page output cache is particularly useful for pages that do not change frequently but require a large amount of processing to be created.
Application cache (data cache)
Coding implementation
2. Implement page cache. Add an OutputCache statement in the page declaration area.
The Duration attribute is used to specify the time when the current page is to be cached, in seconds.
86400 cache for one day
VaryByParam indicates caching the Page Based on the specified query string
None: indicates that the query string is not cached. It is usually used in static pages.
* Indicates cache based on any query string
Multiple query strings are separated by semicolons, such as "ProductID; CategoryID"
3. Use cache configuration <% @ OutputCache CacheProfile = "ProductCache" %>
<Configuration>
<System. web>
<Caching>
<OutputCacheSettings>
<OutputCacheProfiles>
<Add name = "ProductCache" duration = "3600" varyByParam = "none"/>
<Add name = "1 hoursCache" duration = "5000" varyByCustom = "browser"/>
</OutputCacheProfiles>
<OutputCacheSettings>
</Caching>
</System. web>
</Configuration>
4. Use the HttpCachePolicy class to control the cache
Response. Cache. SetCacheability (HttpCachePolicy. Public );
Response. Cache. SetExpires (DateTime. Now. AddSeconds (60 ));
Response. Cache. SetValidUntilExpires (true );
Cache based on browser type
Response. Cache. SetExpires (DateTime. Now. AddMinutes (1d ));
Response. Cache. SetCacheability (HttpCacheability. Public );
Response. Cache. SetValidUntilExpires (true );
Response. Cache. SetVaryByCustom ("browser ");
Using code to control cache is not a good design
5. the most flexible Cache type for caching Application Data
General principle: Do not cache data at will, but return the value to the village for data that is particularly occupied by resources
Cache objects are thread-safe and do not need to be locked or unlocked when writing multi-threaded applications.
If an object in the Cache expires, it will be automatically removed. Therefore, when getting data from the Cache object, you must first determine whether the object to be retrieved exists.
Since the Cache object supports caching, it can be associated with a file, a database table, or any other type of resources. If the Cache object changes, it can be automatically removed or updated.
6. cache dependency class
CacheDependency: allows you to create dependencies Based on files or other caches.
SqlCacheDependency: SQL Server-based tables or SQL Server-based Query Dependencies
AggregateCacheDependency: dependencies Based on Multiple cache dependencies. For example, you can combine both SQL dependencies and file dependencies.
7. Configure SQL cache Dependencies
Use the dependent data library and a single data table, and use the aspnet_regsql.exe command line tool for configuration.
Configure database support cache dependency: aspnet_regsql.exe-S localhost-U sa-d Northwind-ed
Enable cache dependency for the Products table: aspnet_regsql.exe-S localhost-U sa-d Northwind-t Products-et
Configure cache dependencies in the web. config file
<Caching>
<SqlCacheDependency enabled = "true" pollTime = "3600">
<Database>
<Add name = "NorthwindDatabase" connectionStringName = "NorthwindConnectionString"/>
</Database>
</SqlCacheDependency>
</Caching>
8. Use SQL cache dependencies in programs
Use cache dependency in the output Cache
<% @ OutputCache Duration = "1200" SqlDependency = "NorthwindDataBase: Products" %>
To use SQL cache dependencies in the SqldataSource control, you only need to assign the same value to the SqlCacheDependency attribute.
SqlDependency = "NorthwindDataBase: Products"
To use SQL Cache dependencies in Cache objects, You need to instantiate a SqlDependency object.
Protected void EnableDenpendency ()
{
DataSet ds = (DataSet) Cache ["ProductInfo"];
If (ds = null)
{
Ds = GetProductDataSet ();
SqlCacheDependency sqldependency = new SqlCacheDependency ("NorthwindDataBase", "Products ");
Cache. Insert ("ProductInfo", ds, sqldependency );)
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service