The aspnet_regsql tool is located in the Windows \ Microsoft. NET \ framework \ [version folder]
Direct operations in the specified order
Doscommand
Enable database cache Dependencies
Run: aspnet_regsql-s Dev-u username-P password-D cache database name-ed
Check the database generation table: aspnet_sqlcachetablesforchangenotification
Starts cache dependencies on a table in the database.
Run: aspnet_regsql-s Dev-u username-P password-D cache database name-t data table name-et
Check the database table: aspnet_sqlcachetablesforchangenotification to generate a record
-? Displays the help function of the tool;
-S is followed by the database server name or IP address;
-U is followed by the login username of the database;
-P is followed by the database login password;
-E this function is used for Windows integrated verification;
-D the following parameter indicates which database adopts the sqlcachedependency function;
-T followed by the sqlcachedependency function for which table the parameter is used;
-Ed allows sqlcachedependency for databases;
-Dd prohibits the use of sqlcachedependency for databases;
-Et allows sqlcachedependency for data tables;
-DT prohibits the use of sqlcachedependency for data tables;
-Lt lists the tables in the current database that have used the sqlcachedependency function.
3. is ASP.. NET provides query and tracing of data tables. It also adds triggers for tables that use sqlcachedependency, which correspond to insert, update, delete, and other data change-related operations. For example, the trigger of the product data table:
Create trigger DBO. [product_aspnet_sqlcachenotification_trigger] on [data table name]
For insert, update, delete as begin
Set nocount on
Exec DBO. aspnet_sqlcacheupdatechangeidstoredprocedure n 'table name'
End
4. Define the cache factory interface:
Public interface isnsshopcachedependency
{
Aggregatecachedependency getdependency ();
}
Aggregatecachedependency is a new class added by. NET Framework 2.0. It monitors the collection of dependency objects. When any dependency object in this set changes, the cache object corresponding to the dependency object will be automatically removed.
The aggregatecachedependency class combines cachedependency objects. It can associate multiple cachedependency objects or even different types of cachedependency objects with cache items. Isnsshopcachedependency Interface
The getdependency () method is used to return the aggregatecachedependency object that has created these dependencies.
5. cachedependency implementation
The implementation of cachedependency is to create a corresponding sqlcachedependency type dependency for the test data table, as shown in the Code:
Public abstract class tabledependency: ipetshopcachedependency
{
Protected char [] configurationseparator = new char [] {','};
Protected aggregatecachedependency dependency = new aggregatecachedependency ();
Protected tabledependency (string configkey)
{
String dbname = configurationmanager. receivettings ["cachedatabasename"];
String tableconfig = configurationmanager. configurettings [configkey];
String [] tables = tableconfig. Split (configurationseparator );
Foreach (string tablename in tables)
Dependency. Add (New sqlcachedependency (dbname, tablename ));
}
Public aggregatecachedependency getdependency ()
{
Return dependency;
}
}
6. The databases and data tables for which dependencies need to be created are configured in the web. config file. The settings are as follows:
<Add key = "cachedatabasename" value = "SNS"/>
<Add key = "testtabledependency" value = "test"/>
<! -- Enable data cache -->
<Add key = "enablecaching" value = "true"/>
<! -- Dependencyfacade. CS cache dependency option config_cache_assembly -->
<Add key = "cachedependencyassembly" value = "tablecachedependency"/>
<! -- Cache time hour -->
<Add key = "usercacheduration" value = "12"/>
<! -- Experience in the center of the homepage, caching event minute -->
<Add key = "expcacheduration" value = "10"/>
<! -- Name of the cache database -->
<Add key = "cachedatabasename" value = "databasename"/>
<! -- List the table dependencies of each instance separated by commas (,) -->
<Add key = "testtabledependency" value = "test"/>
</Appsettings>
<System. Web>
<! -- Define a cache policy: -->
<Caching>
<Sqlcachedependency enabled = "true">
<Databases>
<Add connectionstringname = "dbsqlconnectionstring" name = "databasename"/>
</Databases>
</Sqlcachedependency>
7. Depending on the dependencies between data tables, the dependencies required for different data tables are also different. The values in the configuration file can be seen. However, no matter how many dependencies are created, the created behavior logic is similar. Therefore, during design,
Abstract A common class tabledependency and create a constructor with parameters to create dependencies. In the implementation of the getdependency () method, the returned object dependency is created in a protected constructor.
Can also be seen as the flexible use of the template method mode. For example, test, a subclass of tabledependency, uses the constructor of the parent class to establish the sqlcachedependency dependency of the test data table:
Public class test: tabledependency
{
Public test (): Base ("testtabledependency "){}
}
If you need to customize cachedependency, you can create dependencies in different ways. However, whether you create a sqlcachedependency object or a custom cachedependency object, add these dependencies
In the aggregatecachedependency class, we can also create a special class for the custom cachedependency, as long as the isnscachedependency interface is implemented.
8. cachedependency Factory
All the test classes that inherit the abstract class tabledependency must create their own objects during the call. Because their parent class tabledependency implements the isnsshopcachedependency interface, they also indirectly implement the isnscachedependency interface,
This provides a prerequisite for implementing the factory model. The configuration file and reflection technology are still used to implement the factory model. In the namespace cachedependencyfactory, dependencyaccess is the factory class for creating an isnscachedependency object:
Public static class dependencyaccess
{
Public static ipetshopcachedependency createitemdependency ()
{
Return loadinstance ("test ");
}
Private Static ipetshopcachedependency loadinstance (string classname)
{
String Path = configurationmanager. etettings ["cachedependencyassembly"];
String fullyqualifiedclass = path + "." + classname;
Return (ipetshopcachedependency) Assembly. Load (PATH). createinstance (fullyqualifiedclass );
}
}
9. Although the dependencyaccess class creates a class test that implements the isnscachedependency interface, the purpose of introducing the isnscachedependency interface is to obtain the aggregatecachedependency object that creates the dependency.
. We can call the interface method getdependency (), aggregatecachedependency dependency = dependencyaccess. createtestdependency (). getdependency () of the object. It seems that we can
The dependencyaccess class is improved by modifying the original createtestdependency () method to the method for creating an aggregatecachedependency object. However, this method disrupts the dependencyaccess responsibilities of the factory class, and
The action of creating an isnscachedependency interface object may still be called by the caller. Therefore, it is necessary to retain the original dependencyaccess class. Introduce the facade mode to make it easier for callers to obtain aggregatecachedependency
Type object.
10. Introduce the facade Mode
The facade mode can be used to encapsulate some complex logics to facilitate callers to call these complex logics. It seems that a uniform facade is provided, which encapsulates internal subsystems into a high-level interface.
The purpose of the facade mode is not to introduce a new function, but to provide a higher level of abstraction based on the existing functions, so that the caller can directly call the function without worrying about the internal implementation method. Take the cachedependency factory as an example.
The user provides an easy way to obtain the aggregatecachedependency object. Therefore, the dependencyfacade class is created:
Public static class dependencyfacade
{
Private Static readonly string Path = configurationmanager. etettings ["cachedependencyassembly"];
Public static aggregatecachedependency gettestdependency ()
{
If (! String. isnullorempty (PATH ))
Return dependencyaccess. createtesdependency (). getdependency ();
Else
Return NULL;
}
}
The dependencyfacade class encapsulates the logic for obtaining the aggregatecachedependency type object. As a result, the caller can call relevant methods to obtain the aggregatecachedependency type object for creating the relevant dependency:
Aggregatecachedependency dependency = dependencyfacade. getcategorydependency ();
Compared to calling the getdependency () method of the dependencyaccess class directly, in addition to the simpler method, it also judges the cachedependencyassembly configuration section. If its value is null, a null object is returned.
11. Call cache dependencies at the business logic layer
Private Static readonly int exptimeout = int. parse (configurationmanager. deleettings ["testcacheduration"]); // timeout
Private Static readonly bool enablecaching = bool. parse (configurationmanager. deleettings ["enablecaching"]); // enable caching
If (! Enablecaching)
{
Return expvew. getrandomexperience (topcount );
}
String key = "exp_index ";
Ilist <exp_experience> expdata = (ilist <exp_experience>) httpruntime. cache [Key];
If (expdata = NULL)
{
Expdata = (ilist <exp_experience>) expvew. getrandomexperience (topcount );
Aggregatecachedependency Cd = dependencyfacade. getexpdependency ();
Httpruntime. cache. Add (Key, expdata, CD, datetime. Now. addminutes (exptimeout), cache. noslidingexpiration, cacheitempriority. High, null );
}
Return expdata;
Refer to Chapter 4 of petshop4.0 file sharing