Caching is to put information in memory to avoid frequent access to the database to extract data from the database, in the system optimization process, the cache is a more common optimization practices and faster practices.
For MVC there is a control cache and an action cache.
One, control cache
The control cache applies the cache to the entire control, and all actions under that control are cached.
Let's take a look at the example:
Ten )] publicclass homecontroller:controller { // GET: Homepublic actionresult Index () { = datetime.now; return View (); } }
In view:
@{ "Index";} Effect:
Constantly refresh the page, the time will be updated every 10 seconds.
Second, action cache
Load the cache on the action so that only the cached action is cached and the other action is not.
The notation is the same as the cache for control.
// action Cache, 10 seconds Ten )] // get:homepublic actionresult Index () { = DateTime.Now; return View (); } Public actionresult Index2 () { = datetime.now; return View (); }
Here are two actions, one with cached index, and one without cache Index2
Effect:
The two pages are accessed separately, and the time in Index is updated 10 seconds while the INDEX2 is updated in real time.
Third, use the configuration file for cache configuration
In the MVC Web. config file, you can configure the cache in a related configuration.
In the system.web node, add the caching child node, and then the following:
<outputCacheSettings> <outputCacheProfiles> <add name="testconfigcache "duration= "/> </outputCacheProfiles> </outputCacheSettings>
Once configured, our control cache or action cache can be written like this:
" Testconfigcache " )] // get:homepublic actionresult Index () { = DateTime.Now; return View (); }
Iv. Cache Dependency
Cached data is obtained from a table in the database, if the data of the corresponding table in the database has not changed, we do not need to update the cached data, if the database corresponding to the table data has changed, then our corresponding cache data should be updated immediately.
If the cache expires, it depends on whether the data in the table corresponding to the database has changed. This is the cache dependency. Let's write it down.
The following is not configured in our MVC Web. config file:
1. First configure the database connection string:
<connectionStrings> <add name="sqlcon" connectionstring= " server=127.0.0.1;database=test;uid=sa;pwd=123456" providername=" System.Data.SqlClient" /> </connectionStrings>
2. Cache Dependent Configuration:
<caching> <sqlCacheDependency> <databases> <add name="personcachedependency"Connectionstringname="Sqlcon"Polltime=" -"/> </databases> </sqlCacheDependency> <outputCacheSettings> <outputcachepr Ofiles> <add name="Testconfigcache"duration="3600"sqldependency="Personcachedependency:person"/> </outputCacheProfiles> </outputCacheSettings> </caching>
Where Polltime is the time interval (in milliseconds) for monitoring database changes
Above configuration description: library name: Test, Listener table name: person. The cache time is 3,600 seconds, which is one hour. The database relies on a period of 500 milliseconds, that is, every 0.5 seconds the database is monitored for changes, and the cache is updated immediately if there is a change.
In control or in action:
" Testconfigcache " )] // get:homepublic actionresult Index () { = DateTime.Now; return View (); }
This allows the cache to update only after the data in the person table has changed in one hours, otherwise the cache will not be updated.
Five, note:
After we have configured the cache, we may have an error message when we run our project:
This is because we have not enabled cache notifications on the person table.
Open the VS command tool line, enter: Aspnet_regsql-s localhost-u sa-p 123456-ed-d test-et-t person
This will resolve the above error.
Well, the MVC cache is introduced here. Insight Shallow, there are inappropriate, but also hope that the great God pointing twos.
Efforts, not to be moved by WHO, nor to do to whom to see, but to allow themselves at any time to be able to jump out of their own aversion to the circle, and have the right to choose. Remember, live your life the way you like it.
On the MVC cache