MVC Cache Summary

Source: Internet
Author: User
Tags configuration settings

Every time come to the garden will see a lot of articles, see is dazzling, overwhelmed, the reason is that they learn too little!! You do not learn, it means that you are gradually facing backward,,, live to learn old. Learning is a habit ...

Look at Daniel Li Lin's garden. A brief application of the MVC cache is described. I'm also deepening my understanding of MVC. In the brief exercise:

Reference to the Garden of Li Lin: The cache is to place information (data or pages) in memory to avoid frequent database storage or to perform the entire page life cycle until the cached information expires or relies on changes to read data from the database again or to re-execute the page's life cycle. In the process of system optimization, caching is a more common optimization practice and a faster approach. Scenario: The data is frequently used and is rarely changed or is not demanding for immediacy.

first, the controller inside the cache: affect all the action in the class duration=10 unit is the second, after the expiration of the update cache trigger

[OutputCache (duration=10)]
public class Studentmanagercontroller:controller
{
Private Studententities db = new studententities ();

Public ViewResult Index ()
{
var stulist = Db.stuList.Include (s = = S.scclass);
Viewbag.scclassid = new SelectList (db.scclasslist, "ID", "className");
Return View (stulist. ToList ());
}

}

Second: Action action cache:

public class Studentmanagercontroller:controller
{
Private Studententities db = new studententities ();

[OutputCache (duration=10)]
Public ActionResult Create ()
{
Viewbag.scclassid = new SelectList (db.scclasslist, "ID", "className");
return View ();
}

}

Third, configure the cache in config file: when there are many aontroller and actions to join the cache, and the parameters are inconsistent, then read the configuration settings through the program.

The configuration file needs to be configured on the system.web cache node as follows:

<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name= "Configcache" duration= "/>"
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>

Read cache in configuration file: If both cache action is used to cache the primary controller cache then

public class Studentmanagercontroller:controller
{
Private Studententities db = new studententities ();

[OutputCache (CacheProfile = "Configcache")]
Public ActionResult Create ()
{
Viewbag.scclassid = new SelectList (db.scclasslist, "ID", "className");
return View ();
}

}

The cache dependency, that is, by configuring the node sqldependency to obtain the associated data base and the table to implement the cache dependency:

1. Invoke the name within the node in the application Sqldepependencycache

public class Studentmanagercontroller:controller
{
Private Studententities db = new studententities ();

[OutputCache (CacheProfile = "Sqldepependencycache")]
Public ActionResult Create ()
{
Viewbag.scclassid = new SelectList (db.scclasslist, "ID", "className");
return View ();
}

}

2. Add database dependencies in the configuration file:

<system.web>
<caching>
<sqlCacheDependency>
<databases>
Add Name= "Usercachedependencycache" connectionstringname= "Conn" polltime= "+"/>
</databases>
</sqlCacheDependency>
<outputCacheSettings>
<outputCacheProfiles>
<add name= "Sqldepependencycache" duration= "" sqldependency= "Usercachedependencycache:student"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>

Interpretation:

* Because of dependent databases, all must contain the database connection node Connectionstrings;conn is the name of the database connection string.

*. Polltime= "40" is the millisecond in which the listener database is changed. i.e. a period of 40 milliseconds;

*. Sqldependency= "Usercachedependencycache:student" is the name of the database-dependent node: Student is the table name note is lowercase, multiple words are separated by commas, Don't forget to also write the name of the original node Usercachedependencycache:student2.

3. Enable the node name for the database:

Open the VS Command window and enter the command: asp_regsql-s localhost-u sa-p 520488-ed-d schooldb-et-t Student

That is, the database address: localhost; User: SA, Password: 520488 database name: SCHOOLDB table name: Student (lowercase).

When you are prompted to finish enabling, when you change the database. The time that the cache is dependent on is also updated.

Citation: Li Lin's Garden

MVC Cache Summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.