1. File Cache dependency.
In terms of caching, file caching is the most free, because it is the best either technically or securely. I have learned from the knowledge that I am free to study this stuff, and now I will write my experiences.
I. I used to learn ASP. Net (mainly for databases ). How can we update the data in the database and synchronize the files outside the database? This is a very troublesome problem. Many people know that a trigger should be written, but how to achieve synchronization is indeed a problem, fortunately, SQL2000 or above, sql2005 has such a stored procedure. sp_makewebtask. if you look at the stored procedure itself, you will find that make_web is called to generate Web files under the corresponding directory, that is, HTML. The detailed usage is as follows:
Create trigger writecachedepfile on [DBO]. [user]
For insert, update, delete
As
Exec sp_makewebtask path, SQL
It has two parameters, path, which is the absolute path of your dependent file and the t_ SQL Statement of SQL. It should be noted that the output results of the SQL statement here will be reflected in the file under the path directory.
Well, the first step has been completed, so let's continue to write the corresponding Asp.net content. For readers to understand, I will first make the simplest (I used previously) Code As an explanation:
Protected static void refreshcache (string key, object item, system. Web. caching. cacheitemremovedreason reason)
{
String mycon = configurationmanager. receivettings ["myconnection"];
Sqlconnection myconnection = new sqlconnection (mycon );
Sqldataadapter dp = new sqldataadapter ("select * from [user]", myconnection );
Dataset DS = new dataset ();
DP. Fill (DS, "[user]");
Cacheitemremovedcallback onremove = new cacheitemremovedcallback (refreshcache );
String defile = system. configuration. configurationmanager. deleetpipeline ["dependencyfile"]. tostring ();
Httpcontext. Current. cache. insert ("[user]", DS, new cachedependency (defile), cache. noabsoluteexpiration, cache. noslidingexpiration, cacheitempriority. High, onremove );
}
Description of cacheitemremovedcallback delegation:
In fact, I think that the benefits and essence of C # are the implementation of the delegation mechanism. This is very useful. I will not talk about it here. Let me continue with the above courses.
The purpose of this delegate is to call the enumeration of the response to the response time if the file changes during the dependency with the file. You can compare it with the trigger.
Cachedependency class is used to obtain the actual meaning of dependencies, including the absolute address of file dependencies.
For more information, see msdn. The following is a call description:
If (Cache ["[user]"] = NULL)
{
Refreshcache (null, null, 0 );
}
Else
{
Response. Write ("conghuancunzhongduqu ");
Dataset Ds;
DS = (Dataset) cache ["[user]"];
Datagrid1.datasource = Ds. Tables ["[user]"]. defaultview;
This. datagrid1.databind ();
}
However, although the above release is useful, it is a lot of trouble for the author to use the three-layer structure development, because the key method has parameters, this is not good, so we have the following method:
Static bool itemremoved = false;
Static cacheitemremovedreason reason;
Cacheitemremovedcallback onremove = NULL;
Protected void page_load (Object sender, eventargs E)
{
BIND ();
}
Public void removedcallback (string K, object v, cacheitemremovedreason R)
{
Itemremoved = true;
Reason = R;
}
Public void additemtocache ()
{
Cachedependency Dep = new cachedependency ("d :\\ ASP. Net project \ sqlcache \ cache.htm", datetime. Now );
string sqlcon = configurationmanager. appsettings ["myconnection"]. tostring ();
sqlconnection con = new sqlconnection (sqlcon);
dataset DS = new dataset ();
string SQL = "select * from [user]";
sqldataadapter dp = new sqldataadapter (SQL, con);
DP. fill (DS, "[user]");
onremove = new cacheitemremovedcallback (this. removedcallback);
If (Cache ["key1"] = NULL)
cache. add ("key1", DS, Dep, datetime. now. addseconds (60), timespan. zero, cacheitempriority. high, onremove);
}< br> Public void BIND ()
{< br> If (Cache ["key1"]! = NULL)
{< br> response. write ("dwad");
gridview1.datasource = cache ["key1"] As dataset;
gridview1.databind ();
}< br> else
{< br> response. write ("111");
additemtocache ();
}< BR >}< br> you can see in detail that this method is very useful. I will not go into detail.
although the preceding file dependency is dependent on the database, it essentially describes how to use the file dependency.
2. SQL dependency Description:
here, the author's SQL dependency description is based on the Program .
first, you must notify you of the changes to the aspsql database.
sqlcachedependencyadmin. enablenotifications (
configurationmanager. etettings ["myconnection"]. tostring ();
declare the database table name with the change notification.
string [] tables = tablename. text. split (New char [] {';'});
for (INT I = 0; I tables [I] = tables [I]. trim ();
sqlcachedependencyadmin. enabletableforconfigurications (
configurationmanager. appsettings ["myconnection"]. tostring (), tables);
in this way, if the table you declare has been updated, inserted, or deleted, the SQL data changes will be sent to the corresponding code for processing.
then add the top of the page you are using to the cache.
<% @ outputcache duration = "3600" sqldependency = "Database Name: Table Name" varybyparam = "NONE" %>