Cache dependencies (files and databases)
Preface
Introduction to the basic Cache Usage: I recommend that you check the asp.net cache.
In this article, I mainly write down the cache dependencies of General SQL statements and the database cache dependencies using Mvc filters.
What is cache dependency?
1. cache: stores the resources you want to access in the memory, occupying a certain amount of memory space. Instead, the user reads data in the memory, reducing the number of times the database or resource files are read, this improves the concurrency of your program and the returned request rate.
2. the cache is not timely: because the data is stored in the memory during the cache time, it is not known whether the data source has changed, but the real-time effect of information is lost.
3. Solve the Problem of timeliness: To solve the problem of the second article, Microsoft considers cache dependency.
4. cache dependency: it is a mechanism in which the cache notifies the cache of expiration by monitoring the reading and writing of dependencies (files or databases. Upload File data. The Code is even more powerful.
Cache dependencies (files)
// The File cache depends on if (cache. get ("key") = null) // if the data in the dependency item changes, the notification cache is cleared (system cleared) {CacheDependency dp = new CacheDependency (Server. mapPath ("/Data/123.txt"); // create cache dependency dp string str = DoIOFile. readFiles ("/Data/111.txt"); cache. insert ("key", str, dp);} Response. write (cache. get ("key"); // data in the Keys File
Effect: the stored data is the data in 111.txt, and the data in 111.txt is changed. The cache with the key is not cleared, that is, the data before modification is still displayed. However, if the data in the dependent item 123.txt changes, the cache will be cleared immediately and the new data will be written to the cache again. This is the benefit of cache dependency. You can try it and I will not fool you.
Cache dependency (folder)
// Folder cache depends on if (cache. get ("key") = null) // if the data in the dependency item changes, the notification cache is cleared (system cleared) {CacheDependency dp = new CacheDependency (Server. mapPath ("/Data"); // creates a cache dependency dp string str = DoIOFile. readFiles ("111.txt"); cache. insert ("key", str, dp);} Response. write (cache. get ("key"); // data in the Keys File
Effect: Here/Data is a folder. All the level-1 files directly under Data (that is, files that cannot be included in the nested folder) will trigger a notification to clear the cache.
Cache dependencies (multiple files)
// Multi-file dependency if (cache. get ("key") = null) // if the data in the dependency item changes, the notification cache is cleared (system cleared) {CacheDependency dp1 = new CacheDependency (Server. mapPath ("/Data/123/123 .txt"); // here is the monitoring file or directory CacheDependency dp2 = new CacheDependency (Server. mapPath ("/Data/123.txt"); CacheDependency [] dps = 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 file in the dependency item changes, the cache is cleared, and the file is written to the new cache.
Cache in Mvc
The cache usage method in mvc is relatively simple. You just need to define it on the filter. I will not describe anything else, which is similar to webForm.
[OutputCache (Duration = 20)] // defines the cache in seconds. Duration is a 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 little complicated and will be followed.
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>
<system.web> <caching> <sqlCacheDependency enabled="true" pollTime="2000"> <databases> <add name="Test" connectionStringName="Am_WeixinWeb" /> </databases> </sqlCacheDependency> </caching>
Note:PollTime, in milliseconds, is used to detect the database every 2 seconds and check whether the table has changed. ConnectionStringName is the database link string.
2. Start database cache dependency
Search for the aspnet_regsql.exe tool in the C drive.
In the command cd: run to the tool file, 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 on which the cache depends
Tool command parameters list see: http://msdn.microsoft.com/zh-cn/library/ms229862
3. Use cache Dependencies
// SQL cache depends on DataSet ds = new DataSet (); if (cache. get ("key") = null) {string conStr = DoXml. readWebConfigConnectionStrings ("Am_WeixinWeb"); SqlConnection conn = new SqlConnection (conStr); string SQL = "select top (1) recContent from Am_recProScheme"; SqlCommand cmd = new SqlCommand (SQL, conn); SqlDataAdapter sda = new SqlDataAdapter (cmd); sda. fill (ds, "tb1"); SqlCacheDependency dep = new SqlCacheDependency ("Test", "Am_recProScheme"); // cache configuration key of the configuration item corresponding to Test, the following figure shows the database table name cache. insert ("key", ds. tables ["tb1"]. rows [0] ["recContent"]. toString (), dep);} Response. write (cache. get ("key "));
Effect: if the data in the table Am_recProScheme in the database Am_WeixinWeb changes, the cache is cleared and the data is written again.
Configure cache dependencies (databases) in the Mvc Filter)
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="2000"> <databases> <add name="Test" connectionStringName="Am_WeixinWeb" /> </databases> </sqlCacheDependency> </caching>
Note:PollTime, in milliseconds, is used to detect the database every 2 seconds and check whether the table has changed. ConnectionStringName is the database link string.
2. Configure the filter
// Mvc cache dependency [OutputCache (Duration = 20, SqlDependency = "Test: Am_recProScheme")] // Test: indicates the cache configuration key, the cache depends on the public ActionResult Index () {Response. write (db. am_recProScheme.FirstOrDefault (). recContent); return View ();}
Effect: if the data in the table Am_recProScheme in the database Am_WeixinWeb changes, the cache is cleared and the data is written again.
This article focuses on practicality and simplicity. If you have any discussions, you can add a technical exchange group in the upper-left corner. Thank you for reading this article. May you have some help.
. Net data cache dependency and File Cache dependency
Virtual Machine ...... Give up.
After self-implementation, the data to be cached is lost to the memory. Then write a class to control the reading and writing of this piece of data. Read data directly from the memory and write data to the database. If the write is successful, modify the memory data. Pay attention to locking data operations.
Add cache dependencies on remote database server files.
Virtual Machine ...... Give up. After self-implementation, the data to be cached is lost to the memory. Then write a class to control the reading and writing of this piece of data. Read data directly from the memory and write data to the database. If the write is successful, modify the memory data. Pay attention to locking data operations.