"Noun explanation"
Cache dependency, the plain English explanation is that the cache is updated depending on the other object. So sqlcachedependency means that the cache's data update relies on changes in SQL Server database tables (row-level changes) or changes in SQL query results.
The use of caches has also been summarized previously
Page Output cache: I use the ASP. OutputCache
Data cache: I cache data with ASP.
But the reliance on the cache is still very vague, and the former is fortunate enough to actually use this class in the project. Now combine their actual and online Daniel's experience, to share with you.
"Use Background"
First you have to use ASP. NET and SQL Server to develop the application, that is, SqlCacheDependency is based on Microsoft's system. Classmate, if you are not engaged in net development, first give a praise and skip it.
Caching is used to improve application performance and reduce server pressure. Suitable for scenarios where data is not easy to change, data is easy to use, and for dynamic query data, such as data analysis, it is best to discard the cache.
"My Code"
SqlCacheDependency the use of SQL Server communication mechanism, when the data table changes, notify the application to delete the original cache, update the new data.
First, you need to check that SQL Server is enabled for Service Broker.
Select databasepropertyex (' Database name ', ' isbrokerenabled ')--1 indicates that 0 is not enabled
Enable statement
ALTER database name SET enable_broker;
Run the Enable statement, there will be suspended animation situation, it does not matter, stop the query, re-query once you can.
Some of the above experience from the Internet, the individual in practice does not appear in these situations, state =0, can still be used. I am using SQL Serve2008, and I personally feel that the high version of SQL Server is no longer required to use these as a requirement (personal opinion, disagreement, welcome discussion). After all, net development is a fool's way of programming.
The next step is to configure Database cache notifications. Two methods (MSDN), the individual recommends the second type.
The first uses the Aspnet_regsql.exe ( located in the windows\microsoft.net\framework\[Version] folder ) tool to configure the database and the table to turn on the cache notification feature. You only need to remember the following command to open:
Aspnet_regsql-s Localhost-e-D rwww-ed-t Options-et
Each command represents what it means, and there is a detailed explanation on the web. Interested students can get to know. After you run the command, the configuration is ready. Here especially need to pay attention to the case, the author in the development of the table name all into uppercase, the result took one hours, the program is always running, has been an error, prompted not to enable the SQL cache notification database. The result was that the case was not correct.
The second is the use of the SqlCacheDependencyAdmin class
SqlCacheDependencyAdmin. enablenotifications (connectionString); Data cache dependency capabilities for starting a database
SqlCacheDependencyAdmin. enabletablefornotifications (connectionString, table); Enable data table caching
Put the two lines of code in the Start method in global.
Next, configure the configuration file.
<caching>
<!--start cache dependency, polling every 5 seconds--
<sqlcachedependency enabled= "true" Polltime= ">"
<databases>
<add name= "Wwwaccount" connectionstringname= "ConnectionString" polltime= "the"/>
</databases>
</sqlCacheDependency>
</caching>
Here is basically a fixed format, it must be configured to do so.
Finally, the cache is added to the code, the nonsense is not much to say, directly on the code. The development of the EF used to the east.
//determine if the cache has been cached if(system.web.httpcontext.current.cache["Link"] ==NULL) { //If there is no cache to retrieve the data varLinks =db. Www_link.asqueryable (); //Add to CacheSystem.Web.HttpContext.Current.Cache.Insert ("Link", links,NewSqlCacheDependency ("Wwwaccount","Www_link") ); //Return Data returnlinks; } //If the cached data is already cached returnsystem.web.httpcontext.current.cache["Link"] asiqueryable<www_link>;
When the table Www_link changes, cache["Link" will be updated automatically. Here's the idea of SqlCacheDependency the first parameter, which equals the node Name=wwwaccount in App. Config.
I use the ASP. SQL data cache dependency (SqlCacheDependency)