MVC cache dependency: file dependency, mvc cache dependency File

Source: Internet
Author: User

MVC cache dependency: file dependency, mvc cache dependency File

"1" indicates cache dependencies. File dependencies mean that I have not set the cache expiration time. When the cached dependent file content changes, the framework is notified to clear the cache. Then, the data in the database is retrieved (or the data in the file) and then cached. When a user requests the data, the data is directly retrieved from the cache, if the content of the files on which the cache depends is changed, the files will be cleared and the cache will be repeated ..

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 {Itcast. CMS. common. logHelper log = new Common. logHelper (); // log4net diary Class Object private static System. web. caching. cache = HttpRuntime. cache; public ActionResult Index () {// If the Cache can be "fmsg" If this parameter does not exist or is null, the data is retrieved from the file. Otherwise, the data is retrieved from the Cache. if (Cache ["fmsg"] = null) {// cache the path of the file to be dependent (I am writing an absolute path here, you can also write a virtual path, and then convert it to a physical path) string filePath = System. 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 "); // It does not matter if this code has no cache dependency. Here, we only read the file in the above path (it will be used as the cache value at that time, of course, you can also set other values. Here is just a case.) string msg = System. IO. file. readAllText (filePath, System. text. encoding. default); // create a cache dependent object (initialize the object with the physical path of the server where the cached dependent file is located. As long as the file content changes, it will notify the framework to clear the cache System. web. caching. cacheDependency cDep = new System. web. caching. cacheDependency (filePath); // The first parameter: cache key // The second parameter: cache value // The third parameter: cache dependent object // The fourth parameter: the absolute expiration time of the cache, starting from the first request of the user (because we use the cache dependency here, So NoAbsoluteExpiration indicates that there is no expiration time, that is, it never expires) // fifth parameter: the cache's adjustable expiration time, which starts timing from the last request (because we use cache dependencies here, So NoSlidingExpiration is used here, indicating there is no adjustable expiration time) // The sixth parameter: Specify System. web. caching. the relative priority of items stored in the Cache object. (It is an enumeration) Normal is the default value // The Seventh parameter: the callback function on which the cache depends (this method is called when the cache is cleared or modified) // [in fact, the seventh parameter is a delegate. Since it is a delegate, therefore, when using it, we need to pass a method signature to it in the same way as the delegate signature.] // Cache. the Insert () method can also use Cache. the Add () method is replaced by the Cache. the Add () method has a large number of parameters. Sometimes we use Cache when we don't need so many parameters. insert (), more convenient Cache. insert ("fmsg", msg, cDep, System. web. caching. cache. noAbsoluteExpiration, System. web. caching. cache. noSlidingExpiration, System. web. caching. cacheItemPriority. normal, CacheCallBack); ViewData ["msg"] = msg;} else {// If data exists in the cache where the key is "fmsg, assign it to ViewData ["msg"] ViewData ["msg"] = Cache ["fmsg"]. toString () ;}return View () ;}/// <summary> /// callback function on which the cache depends (this method is called when the cache is cleared) /// </summary> /// <param name = "key"> cache key </param> /// <param name = "value"> cached value </param> // <param name = "reason"> reason why the cache is removed </param> void CacheCallBack (string key, object value, CacheItemRemovedReason reason) {// use the logHelper object to record the log. info ("in the Index method of the Home controller, Cache [" + key + "] =" + value. toString () + "because" + reason. toString () + "deleted ");}}}


Zookeeper

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.