ASP. NET MVC implements dependency caching with SQL Server

Source: Internet
Author: User
Tags connectionstrings

This article is intended to understand the ability of ASP. NET MVC to implement a caching-dependent function with SQL Server and a record of its own learning process for this knowledge. Because I am also the first to understand this knowledge point, what errors, but also hope that the master generous enlighten oh. OK, here's a formal entry topic.

1. Testing environment and tools: vs2012 sqlserver2008r2

2, testing purposes: When the value of the database changes, the page cache will be updated in a timely manner.

Specific steps:

1. Create an empty project of ASP. NET MVC in VS2012.

2. Create a new HomeController and index attempt. The code is simple, as follows

 Public class Homecontroller:controller    {             public  actionresult Index ()        {            return  View ();        }    }

The view code is simple, is the output system current time, so that the test can clearly determine the access to the current page, is the cached data, or the latest data returned from the server.

@{Layout = null;}<!DOCTYPE HTML><HTML><Head>    <title>Index</title></Head><Body>    <Div>@{Response.Write (DateTime.Now.ToString ());} </Div></Body></HTML>

3, the configuration of the Web. config file, mainly the <system.web> node under the <caching> node configuration, the code is as follows

< connectionStrings >        <  name= "testconnectionstring"  connectionString= "Data source=.;i Nitial catalog=test; Persist Security info=true; User Id=sa; Password=sasa "/></connectionStrings>
<Caching>      <SqlCacheDependencyenabled= "true"Polltime= "$">        <databases>          <Addname= "Test"connectionStringName= "Testconnectionstring"/>        </databases>      </SqlCacheDependency> </Caching>

About the properties of <sqlCacheDependency>, I will not introduce in detail, the relevant information you can check it.

4, the SQLSERVER2008 data cache depends on the function configuration.

Check if Service Broker is turned on: Select databasepropertyex (' Test ', ' isbrokerenabled ') If 1 indicates success, 0 means unsuccessful.

If execution is not turned on

ALTER DATABASE Test Setenable_brokergo

A table is enabled for SQL cache dependency, which means that the cache information is updated when the data for the table is changed. Mainly used in the Asp.net_regsql.exe, the file is stored in the C:\Windows\Microsoft.NET\Framework\v4.0.30319.

Usage: Open the command prompt tool, go to the directory where the Asp.net_regsql.exe is located, execute the command

Aspnet_regsql.exe-s <Server> u <Username>-P <Password>-ed-d <databaseName>-et-t <tablena Me>
Replace the <> in the parameters of your own test. The commands I perform when I test are as follows:
Aspnet_regsql.exe-s. -U sa-p sasa-ed-d test-et-t yh
Successful opening as

5. To add a cached policy for homecontroller action, it is simple to add [OutputCache (Duration = SqlDependency = "Test:yh")] tag on an action

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacemvcapplication1.controllers{ Public classHomecontroller:controller {[OutputCache (Duration= the, SqlDependency ="Test:yh")]         PublicActionResult Index () {returnView (); }    }}

After the above steps, the entire function preparation has ended, run the program, below to see the effect. The first load rendered page is the following time, and then after the refresh, the time remains as shown below, proving that the program's caching function is in effect. So is the cache reset when the table data is modified to be set to dependent?

Next, I in the database table in the following table to modify the data, immediately refresh the page, see, Time has changed, then continue to refresh the page, to see whether the time or the time, that is, modify the value of the data table, the cache has been updated immediately.

You can follow the test.

ASP. NET MVC implements dependency caching with SQL Server

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.