Most of the time, our server performance bottleneck is when querying the database, so the caching of the database is very important, then there is a way to implement the SQL Server database cache, when the datasheet is not updated, read from the cache, when there is an update, to read from the datasheet, The answer is yes, so we can cache some of the most common basic data tables, such as news system news categories, and so on, each time you do not need to read from the database, speed up the site's access speed.
So how do I turn on SQL Server database cache dependencies, as follows:
The first step: Modify the configuration of the Web.config <system.web> section, the code below, let the Web site project enable SqlCacheDependency. Note the connectionStringName in the following code, which is the name of the database connection string variable in the specified <connectionStrings> section. Name is the SqlCacheDependency, which will be used in the third step. The SqlCacheDependency class automatically reads this configuration section information to establish a connection to the database.
Copy Code code as follows:
<system.web>
<add verb= "*" Path= "*.aspx"
Type= "Urlrewriter.rewriterfactoryhandler, Urlrewriter"/>
<add verb= "*" Path= "*.shtml"
Type= "Urlrewriter.rewriterfactoryhandler, Urlrewriter"/>
<add verb= "*" Path= "*.bobo"
Type= "Urlrewriter.rewriterfactoryhandler, Urlrewriter"/>
<!--> The following sets the database cache dependency mode-->
<caching>
<sqlcachedependency enabled= "true" polltime= "6000" >
<databases>
<add name= "Yd_jwc_jake" connectionstringname= "Cachestr"/>
</databases>
</sqlCacheDependency>
</caching>
<!--
Set compilation debug= "True" to insert debug symbols
Pages that have been compiled. But since this will
Affect performance, so this value is only in the development process
Set to True.
-->
<compilation debug= "true" >
<assemblies>
<add assembly= "System.Design, version=2.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a"/>
</assemblies>
</compilation>
<!--
The <authentication> section allows you to configure the asp.net used by
Secure authentication Mode,
To identify the incoming user.
-->
<authentication mode= "Forms" >
<forms loginurl= "Login.aspx" name= ". Ajsupcxaiuth "></forms>
</authentication>
<authorization>
<allow users= "*"/>
</authorization>
<!--
If an unhandled error occurs during the execution of the request,
The <customErrors> section allows you to configure the appropriate processing steps. Specifically
The section enables developers to configure
HTML error page to display
In place of the error stack trace. -->
<customerrors mode= "RemoteOnly" defaultredirect= "/er3.shtml" >
<error statuscode= "403" redirect= "/er1.shtml"/>
<error statuscode= "404" redirect= "/er404.shtml"/>
</customErrors>
</system.web>
Step Two: Execute the following command in CMD to open the SQL Server database support for SqlCacheDependency, using the Aspnet_regsql.exe tool, located in Windows\microsoft.net\ framework\[Version] Folder
The code is as follows:
Copy Code code as follows:
Aspnet_regsql-c "Data source=127.0.0.1;initial catalog=yd_jwc_jake;user id=sa;password="-ed-et-t "T_NewsClass"
Parameter-C followed by the database connection string, note that the letter C is uppercase. Parameter-T followed by the data table in which you want to open the database cache, where I turn on caching dependencies for the News category table. (If you have more than one table, repeat this command, and be careful to modify your datasheet name)
Step three: In the business layer code that gets the data, if it is the first read, it is read from the database and stored in the cache. When you get the data later, the database automatically determines whether the table has updated data, and if so, the read database updates the cache and, if there is no update, reads from the database. The code is as follows:
Copy Code code as follows:
private void Getinfoclass (int t)
{
String cachekey = "Cacheclass" + t.tostring ();
Object objmodle = Jake.DataCache.GetCache (CacheKey);//get from cache
DataTable Dt=null;
if (Objmodle = null)//If the cache does not read the database
{
Jake.BLL.NewsManage.NewsClass NC = new Jake.BLL.NewsManage.NewsClass ();
DT = NC. GetList (""). Tables[0];
Objmodle = DT;
if (Objmodle!= null)
{
System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency ("Yd_jwc_jake", "T_newsclass") ;
Jake.DataCache.SetCache (CacheKey, Objmodle, DEP);
}
}
Else
{
DT = (DataTable) objmodle; Direct read cache in cache, no access to database
}
datarow[] DRS = dt. Select ("", "ClassID");
StringBuilder SB =new StringBuilder ();
Sb. Append ("<ul>");
foreach (DataRow R in Drs)
{
String cid=r["ClassId"]. ToString ();
Security js = new security ();
String decrystr = Jake.Common.ConfigHelper.GetConfigString ("Decrystr");//Get Encryption Key
CID = js. Encryptquerystring (CID, DECRYSTR);
String cdesc=r["Classdesc"]. ToString ();
if (t = = 1)
{
Sb. Append ("<li><a href="/info "+ CID +". shtml "href=" Info "+ CID +". shtml "><span class= ' FontBold ' >" + CDE SC + "</span></a></li>");
}
else if (t = = 2)
{
Sb. Append ("<li><a href="/file "+ CID +". shtml "href=" File "+ CID +". shtml "><span class= ' FontBold ' >" + cdes C + "</span></a></li>");
}
Else
Sb. Append ("<li><a href="/faq "+ CID +". shtml "href=" FAQ "+ CID +". shtml "><span class= ' FontBold ' >" + cdesc + "</span></a></li>");
}
Sb. Append ("</ul>");
Response.Write (SB);
}
The Jake.DataCache.GetCache () method in the above code is a common method that you define to get and set the cache, which is compiled separately into a DLL:
The code is as follows:
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Web;
Using System.Text;
Namespace Jake.
{
public class Datacache
{
<summary>
Gets the cache value of the current application-specified CacheKey
</summary>
<param name= "CacheKey" ></param>
<returns></returns>
public static Object GetCache (String cachekey)
{
System.Web.Caching.Cache Objcache = Httpruntime.cache;
return Objcache[cachekey];
}
<summary>
Sets the cache value for the current application-specified CacheKey
</summary>
<param name= "CacheKey" ></param>
<param name= "Objobject" ></param>
public static void Setcache (String CacheKey, Object Objobject)
{
System.Web.Caching.Cache Objcache = Httpruntime.cache;
Objcache.insert (CacheKey, objobject);
}
<summary>
To set cached dependencies in a way that caches data
</summary>
<param name= "CacheKey" > Key value </param>
<param name= "Objobject" > Cache object </param>
<param name= "DEP" > Cache dependencies </param>
public static void Setcache (String CacheKey, Object Objobject, System.Web.Caching.CacheDependency dep)
{
System.Web.Caching.Cache Objcache = Httpruntime.cache;
Objcache.insert (
CacheKey,
Objobject,
Dep
system.web.caching.cache.noabsoluteexpiration,//never Expires
system.web.caching.cache.noslidingexpiration,//Disable Adjustable expiration
System.Web.Caching.CacheItemPriority.Default,
Null
);
}
}
}
At this point, the cache dependency on the datasheet has been turned on, which can greatly speed up Web site access.
(reprint please indicate the origin of this article: Http://blog.csdn.net/j_jake)