Cache learning records
I have read a learning article about MVC cache. I hope you can learn it together.
Address: http://www.cnblogs.com/iamlilinfeng/p/4419362.html
Cache introduction:
My personal understanding of cache is like this. The user accesses A page and finds A. cshtml at the backend ~ Conversion ~ Serialization... (I don't know much about it either ~ And then give these things to the user's interface. However, if the title of this page is bound to a database, you need to perform database query and then construct it every time you access this page ~ Conversion ~ Serialization .... Isn't it difficult to give the user a page ?. If the user visits this page for the first time, we construct this page ~ Conversion ~ Serialization... After the finished product is saved, the product is directly delivered to the finished product whenever the user wants it. Isn't that much faster?
Controller Cache
This cache caches all the methods under the controller (I tested that not only the page can be cached, but the method can also be cached)
The Controller code is as follows:
The page code is as follows:
From the above, we can see that the Index page displays a time and outputs The Time of the Mess Method Request in the console using ajax requests.
The following figure shows the running effect:
As long as the page is refreshed and browsed within 10 seconds, this time will not change (but it took 2 S from the page generation to the end of the ajax request ...)
About database Dependencies
At the beginning, A piece of data on page A needs to access the database. In this case, the cache is not only dependent on the refresh time, but on whether the database is updated.
Add the node according to the above rules, and then run it in the command line of.
Aspnet_regsql-S localhost-U sa-P 123456-ed-d wcfDemo-et-t user
Mid-Term
-S localhost: Database address
-U sa: Database Login Name
-P 123456: Database logon Password
-D wcfDemo: Database Name
-T user: Table Name (lower case)
In this way, when the database is refreshed, the page cache will expire and be retained ~
Link: http://www.cnblogs.com/iamlilinfeng/p/4419362.html
In addition, the method that does not need to be started is supplemented.
// Enable change notification
SqlCacheDependencyAdmin. EnableNotifications (System. Configuration. ConfigurationManager. ConnectionStrings ["DefaultConnection"]. ConnectionString );
// Connect to the SQL Server database and prepare the database table for the SqlCacheDependency Change Notification
SqlCacheDependencyAdmin. enabletableforconfigurications (System. Configuration. ConfigurationManager. ConnectionStrings ["DefaultConnection"]. ConnectionString, "QJInfo_SiteInfo ");
// Formulate cache policies
SqlCacheDependency scd = new SqlCacheDependency ("SiteInfoSqlDependency", "QJInfo_SiteInfo ");
// Insert Cache
Cache. Insert ("SiteInfo", siteInfo, scd, DateTime. Now. AddHours (5), Cache. NoSlidingExpiration );
When a database table is inserted, updated, or deleted, the cache automatically becomes invalid. An additional table aspnet_sqlcachetablesforchangenotifis added to the database to record the change information of the relevant table.