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 Dependency if(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=NewCacheDependency (Server.MapPath ("/data/123.txt"));//To establish a cache dependency DP stringstr = Doiofile.readfiles ("/data/111.txt"); Cache. Insert ("Key", STR, DP); } Response.Write (Cache. Get ("Key"));//If the content of the 123.txt file is constant, the data in the cache is read, and once the data in the 123.txt file is changed, the data in the 111.txt file is re-read.
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 Dependency if(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=NewCacheDependency (Server.MapPath ("/data"));//To establish a cache dependency DP stringstr = Doiofile.readfiles ("111.txt"); Cache. Insert ("Key", STR, DP); } Response.Write (Cache. Get ("Key"));//If the content of the 123.txt file is constant, the data in the cache is read, and once the data in the 123.txt file is changed, the data in the 111.txt file is re-read.
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 dependencies if(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=NewCacheDependency (Server.MapPath ("/data/123/123.txt"));//here is the monitor file or directoryCacheDependency DP2 =NewCacheDependency (Server.MapPath ("/data/123.txt")); cachedependency[] DPS=Newcachedependency[] {dp1, DP2}; AggregateCacheDependency aDp=NewAggregateCacheDependency ();//Multiple DependenciesAdp.add (DPS); stringstr = 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"); Response.Write (str); 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= "Data source=192.168.1.200;initial catalog=am_weixinweb;uid=sa;password=lh1234; " /> </ connectionStrings >
<system.web> <Caching> <SqlCacheDependencyenabled= "true"Polltime= "$"> <databases> <Addname= "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. 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 DependencyDataSet ds =NewDataSet (); if(Cache. Get ("Key") ==NULL) { stringConstr = Doxml.readwebconfigconnectionstrings ("Am_weixinweb"); SqlConnection Conn=NewSqlConnection (CONSTR); stringsql ="Select Top (1) reccontent from Am_recproscheme"; SqlCommand cmd=NewSqlCommand (SQL, conn); SqlDataAdapter SDA=NewSqlDataAdapter (CMD); Sda. Fill (DS,"tb1"); SqlCacheDependency DEP=NewSqlCacheDependency ("Test","Am_recproscheme");//the cache configuration key for the test corresponding configuration item, followed by the database table nameCache. Insert ("Key"Ds. tables["tb1"]. rows[0]["reccontent"]. 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= connectionstring=" data source=192.168.1.200;initial catalog=am_weixinweb;uid=sa;password= lh1234; /> </connectionstrings>
<caching> <sqlcachedependency enabled="true" polltime="2000 "> <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
// MVC cache Dependency - " Test:am_recproscheme " // Test: The key that is configured for the cache followed by the cache dependency table Public actionresult Index () { Response.Write (db. Am_recproscheme.firstordefault (). reccontent); return View (); }
Effect: The data in the table Am_recproscheme in the database am_weixinweb is changed, then the cache is emptied and re-written.
This article to the practical brief mainly, if has the discussion, may add the top left technical Exchange Group, thanks Reading, may give you a little help.