Cache dependency (file, database)

Source: Internet
Author: User
Tags connectionstrings

Preface

The basic usage of caching is explained: I recommend looking at the ASP.

In this article, I mainly write down the general SQL cache dependency, as well as the use of MVC Filter Database cache dependency.

what is cache dependency

1. Cache: is to put the resources you want to access, in memory, occupy a certain amount of memory space, so that the user read in-memory data, thereby reducing the number of read database, or resource files, thus to your program concurrency, and the rate of return requests to improve a mechanism.

2. Cache is not timely: due to the time of the cache, the data is placed in memory, do not know whether the data source has changed, so that the information lost immediate effect.

3. Resolve the non-timeliness: To solve the second article of the non-timeliness, Microsoft is thinking of the cache depends on

4. Cache dependency: is a mechanism by which the cache is notified of the expiration of a cache by monitoring the read and write of a dependency (file or database). For example, a dependency is a 123.txt file, and the cached data is data in 234.txt, so the caching mechanism can remove data from the cached 234.txt file by monitoring the change in the data in the 123.txt file. Feel the drag, or the code more to force.

cache Dependencies (files)
            //File Cache Dependencyif (cache. Get ("Key") ==Null//If the data in the dependency changes, it is notified that the cache is emptied (the system completes emptying){CacheDependency DP =new cachedependency (Server.MapPath ( "/data/123.txt//string str = Doiofile.readfiles ( "/data/111.txt "); Cache. Insert ( "key" , STR, DP); } Response.Write (Cache. Get ( "key"); // If the contents of the 123.txt file remain intact, the data in the cache is read, once the data in the 123.txt file is changed, the 111.TX is read again. T file data                

Effect: The cached data is data in 111.txt, the data in 111.txt changes, the key of the cache will not be emptied, that is, still show the data before the change. However, if the data in the cache dependency 123.txt changes, the cache is emptied immediately, and the new data in the cache is re-written. That's the benefit of caching dependencies that you can try and I don't fool you.

cache Dependencies (folders)
            //Folder Cache Dependencyif (cache. Get ("Key") ==Null//If the data in the dependency changes, it is notified that the cache is emptied (the system completes emptying){CacheDependency DP =new cachedependency (Server.MapPath ( "/data//string str = Doiofile.readfiles ( "111.txt  "); Cache. Insert ( "key" , STR, DP); } Response.Write (Cache. Get ( "key"); // If the contents of the 123.txt file remain intact, the data in the cache is read, once the data in the 123.txt file is changed, the 111.TX is read again. T file data                

Effect: Here/data is a folder, he directly under the data all the first level files (that is not the nested folder files) If there is a change, will trigger the notification, empty the cache.

cache Dependencies (multiple files)
            //Multi-file dependenciesif (cache. Get ("Key") ==Null//If the data in the dependency changes, it is notified that the cache is emptied (the system completes emptying){CacheDependency DP1 =New CacheDependency (Server.MapPath ("/data/123/123.txt"));//Here is the monitor file or directory CacheDependency DP2 =New CacheDependency (Server.MapPath ("/data/123.txt"new cachedependency[] {DP1, DP2} ; AggregateCacheDependency aDp = new aggregatecachedependency (); // multiple dependencies  Adp.add (DPS); string str = doiofile.readfiles ( "111.txt "); Cache. Insert ( "key" , str, aDp); } Response.Write (Cache. Get ( "key");    

Effect: Any one of the files in the dependency is changed, the cache is emptied, and the new cache is written.

Caching in MVC

The use of the cache in MVC is relatively simple, just use the filter to define on the line, the other I do not tired, and WebForm is no different.

        defines the cache, in seconds, duration is required public         actionresult Index ()        {            string str = Doiofile.readfiles ( "/111.txt"return View ();        

Detailed configuration See: http://msdn.microsoft.com/zh-cn/library/system.web.mvc.outputcacheattribute.aspx

Cache Dependency (database table)

This is a bit cumbersome, then do.

1. Open the project configuration file

<connectionStrings>         <name= "Am_weixinweb" connectionString</  ConnectionStrings>        
<System.Web><Caching><SqlCacheDependencyEnabled= "true" Polltime= "" > Span style= "color: #0000ff;" ><databases> add name= "Test" Span style= "color: #ff0000;" > Connectionstringname= "Am_weixinweb" /> </databases> sqlcachedependency> </caching>  

Note:polltime, milliseconds, consciousness is every 2 seconds to detect the database, check whether the table has changed. connectionStringName the database link string.

2. Start Database Cache dependency

In the C drive, search for the tool aspnet_regsql.exe

In the command CD: Under the file that runs to this tool, type the following command

Aspnet_regsql-c "Data source=;initial catalog=codematic;user id=sa;password="-ed-et-t "T_table "

Parameter:-C followed by the connection string,-t followed by the table name that established the cache dependency

Tool command parameter list see: http://msdn.microsoft.com/zh-cn/library/ms229862

3. Using cache dependencies

            //SQL cache dependent DataSet ds =NewDataSet ();if (cache. Get ("Key") ==Null) {String constr = Doxml.readwebconfigconnectionstrings ("Am_weixinweb"); SqlConnection conn =NewSqlConnection (CONSTR);String sql ="Select Top (1) reccontent from Am_recproscheme"; SqlCommand cmd =NewSqlCommand (SQL, conn); SqlDataAdapter SDA =NewSqlDataAdapter (CMD); Sda. Fill (DS,"Tb1"); SqlCacheDependency dep =New SqlCacheDependency ( "test "am_recproscheme"); //key", ds. Tables[ "tb1"]. Rows[0][reccontent< Span style= "color: #800000;" > "". ToString (), DEP); } Response.Write (Cache. Get ( "key");    

Effect: The data in the table Am_recproscheme in the database am_weixinweb is changed, then the cache is emptied and re-written.

Configure cache dependencies in the MVC Filter (database)

1. Open the project configuration file

<connectionStrings>        <add name="am_weixinweb" connectionstring="data source= 192.168.1.200;initial catalog=am_weixinweb;uid=sa;password=lh1234; "/> </connectionStrings>     
<caching>      <sqlcachedependency enabled="true" Polltime="$"> < databases> <add name="Test" connectionstringname="am_weixinweb"/> </ databases> </sqlCacheDependency> </caching>          

Note:polltime, milliseconds, consciousness is every 2 seconds to detect the database, check whether the table has changed. connectionStringName the database link string.

2. Configure Filters

        the MVC cache relies on        "test:am_recproscheme/publicreturn View ();}     

Effect: The data in the table Am_recproscheme in the database am_weixinweb is changed, then the cache is emptied and re-written.

Cache dependency (file, database)

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.