Asp.net cache, asp.net
Cache can be divided into: Output cache and data cache
Output cache ):
Page cache:
Obviously, it is the page for caching HTML output. When the same page is requested again, asp.net will use the cached page.
<% @ OutputCache Duration = "60" VaryByParam = "None" %>
The above column sub-surface page will be cached for 60 seconds, and users' requests will be cached. After 60 seconds, the cache will expire and the next cache will be created. The VaryByParam attribute is required, if no parameter is set to None.
<% @ OuputCache Durationj = "60" VaryByParam = "id" %>
The preceding column indicates that data is cached based on different IDs.
Custom cache:
You can customize programs to cache pages. ASP. NET provides a convenient way to create a custom cache. You can use the VarByCustom attribute to specify the name of the custom cache type.
<% @ OutputCacheDuration = "60" VaryByParam = "None" VaryByCustom = "browser" %>
You also need to create a method to generate a custom string for the cache, as follows:
Public override string GetVaryByCustomString (HttpContext context, string custom)
{
If (custom = "browser ")
{
Return context. Request. Browser. Browser + context. Request. Browser. MajorVersion;
}
Else
{
Return base. GetVaryByCustomString (context, custom );
}
}
This method must be written in the global. asax file. ASP.. NET. If this method returns the same string in different requests, ASP. NET will use the cached page, otherwise a new Cache version will be generated.
In the preceding example, the GetVaryByCustomString () method creates a cache String Based on the browser name. ASP. NET creates caches of different versions based on different browser requests.
Control cache:
Specifies the content of the Cache Control, which can be achieved through the VaryByControl attribute.
<% @ OutputCacheDuration = "60" VaryByControl = "MyControl" %>
The above Code indicates that asp.net will cache the MyControl for 60 minutes.
Configure File Cache:
You can use the CacheProfile attribute in OutputCache to specify the settings in the configuration file.
<% @ OutputCache CacheProfile = "ProductCacheProfile" VaryByParam = "None" %>
<System. web>
<Caching>
<OutputCacheSettings>
<OutputCacheProfiles>
<Addnameaddname = "ProductCacheProfile" duration = "60"/>
</OutputCacheProfiles>
</OutputCacheSettings>
</Caching>
</System. web>
Data Cache (DataCache ):
For some time-consuming data, we can store it in an object cache set using the key value, as shown below:
Cache ["Name"] = data;
You can use the Cache. Insert () method to set Cache expiration, priority, and dependencies.
Date1 = DateTime. Now;
Cache. insert ("Date1", date1, null, DateTime. now. addSeconds (20), TimeSpan. zero); ASP. NET allows you to set an absolute expiration time or sliding expiration time, but it cannot be used at the same time.
Cache dependency:
Cache dependency indicates that the cache automatically expires when other dependent resources change.
Date2 = DateTime. Now;
String [] cacheKeys = {"Date1 "};
CacheDependency cacheDepn = newCacheDependency (null, cacheKeys );
Cache. Insert ("Date2", date2, cacheDepn );
In the preceding example, the "Date2" cache object depends on the "Date1" cache entry. When the "Date1" Object expires, "Date2" will automatically expire. The first parameter in CacheDependency (null, cacheKeys) is null because we only monitor changes to the cache key.
Cache priority:
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Cache cache = HttpContext. Current. Cache;
Cache. Add ("MyData", "Cache importance level", null, cache. NoAbsoluteExpiration, TimeSpan. FromSeconds (30), CacheItemPriority. High, null );
}
}
This enumeration has a total of six levels
Priority
Low = 1
BelowNormal = 2
Normal = 3
Default = 3
AboveNormal = 4
High = 5
NotRemoveable = 6
Sqldatabase cache dependency:
When a table in the database changes, the data is read from the table. If the table does not change, the data is read from the cache.
<Caching>
<SqlCacheDependency>
<Databases>
<Add name = "mySqlCache" connectionStringName = "SQLCaheTestConnectionString"/>
</Databases>
</SqlCacheDependency>
</Caching>
The parameter of the connectionStringName attribute is the database connection string;
A ). provide the program service for creating membership for the created database named NBAData (if the input database name does not exist or is not written, a new database or a default aspnetdb database will be created). Here, log on to the SQL Server for authentication and use the Visual Studio 2008 command prompt tool:
Aspnet_regsql.exe-S localhost-U sa-P @ ssw0rd-d NBAData-A m
B) enable cache dependency for a table Player in the database NBAData. If cache dependency is not enabled for the database, enter the command-ed to enable cache dependency for the database:
Aspnet_regsql.exe-S localhost-U sa-P @ ssw0rd-d NBAData-ed-et-t Player
Aspnet_regsql.exe-S localhost-E-ssadd-sstype c-d yourDbName
After the cache dependency is successfully added, a table named AspNet_SqlCacheTablesForChangeNotification is added to the database, and a row of records exists.
3. Write <% @ OutputCache Duration = "20" VaryByParam = "*" SqlDependency = "SQLCaheTestConnectionString: Player" %> in the header of the page file to check whether the Player table of the database changes