"1" The so-called cache-dependent, file-dependent means, I do not set the cache expiration time, when the cache depends on the content of the file changes, notify the framework to clear the cache. Then the database to take the data (or file to fetch data) and then take the data cache, the user requests, directly from the cache to fetch data, if the cache depends on the contents of the file changes, and then empty, the cycle.
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Caching;using System.web.mvc;namespace itcast.cms.webapp.controllers{public class Cachetestcontroller:controller {Itcas T.cms. Common.loghelper log = new Common.loghelper ();//log4net journal class object private static System.Web.Caching.Cache Cache = Httpru Ntime. Cache; Public ActionResult Index () {//If the cache that can be "fmsg" does not exist or is empty, go to the file to fetch the data, otherwise go to the cache to fetch the data if (cache["fmsg"] = = null) {//cache the path of the file to depend on (I'm writing an absolute path, or I can write a virtual path and then turn it into a physical path) string FilePath = Sy Stem. Io. Path.GetFullPath (@ "D:\ Enterprise Management System \itcast.cms\itcast.cms.webapp\log\loginfo\2015-05-06.txt"); String filePath = Server.MapPath ("~/master/wowo.txt"); This code does not have a cache dependency is not related, here is just the path above the file read out (then as the value of the cache, of course you can also set other values, here is just a case only) string msg = System.IO.File.ReadAll Text (FilePath, System.Text.Encoding.Default); Create a cache dependent object (and initialize it with the physical path to the server side of the cache-dependent file. As soon as the contents of the file change, it informs the framework to empty the cache System.Web.Caching.CacheDependency CDEP = new System.Web.Caching.CacheDependen Cy (FilePath); First parameter: Cached key//second parameter: Cached value//third parameter: Cache dependent object//Fourth parameter: Absolute expiration of cache, starting from user's first request (because here We are using a cache dependency, so here is the use of noabsoluteexpiration, which means there is no expiry time, i.e. never expires)///Fifth parameter: The cache's adjustable expiration time, starting with the user's last request (because here we are using cache dependency, so here we use n Oslidingexpiration, indicating no adjustable expiration time)//Sixth parameter: Specifies the relative priority of the items stored in the System.Web.Caching.Cache object. (It is an enumeration) Normal is the default value//Seventh parameter: Cache dependent callback Function (call this method when the cache is purged or modified)//"Actually the seventh parameter it is a delegate, since it is a delegate, so we use it when we need to pass a method to sign The name is the same as this delegate signature. The//cache.insert () method can also be substituted with the Cache.Add () method, except that the Cache.Add () method has a number of arguments, and sometimes we use cache.ins when we don't need so many arguments. ERT (), more convenient Cache.Insert ("Fmsg", MSG, CDEP, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Cachin G.cache.noslidingexpiration, System.Web.Caching.CacheItemPriority.Normal, Cachecallback); viewdata["msg"] = msg; } else {//If the key is "fmsg" the cache has data, assign it to viewdata["MSG"] viewdata["msg"] = cache["Fmsg"]. ToString (); } return View (); }//<summary>///cache-Dependent callback function (this method is called when the cache is purged)///</summary>//<param name= "Ke Y "> Cached keys </param>//<param name=" value "> Cached values </param>//<param name=" Reason "> Cache is Reason for removal </param> void Cachecallback (String key, object value, CacheItemRemovedReason reason) { is to use the Loghelper object to record the journal log. Info ("in the index method under the home controller, cache[" + key + "]=" + value.) ToString () + "because" +reason. ToString () + "deleted"); } }}
MVC cache Dependency: file dependencies