ASP. NET cache data skills: access the cache Value Because the information stored in the cache is easy to lose, that is, the information may be removed from ASP. NET, we recommend that you first determine whether the information is cached. If not, add it to the cache again and then retrieve the item.
The following is a code snippet: String cachedstring; If (Cache ["cacheitem"]! = NULL) { Cachedstring = (string) cache ["cacheitem"]; } Else {// When the cache does not exist Cache. insert ("cacheitem", "Hello, world .") Cachedstring = (string) cache ["cacheitem"]; } |
ASP. NET cache data skills: delete cache items Data in the cache may be automatically removed for any of the following reasons: the cache is full, this item has expired, and the dependency has been changed. Note: If you call the insert method and add an item with the same name as an existing item to the cache, the old item will be deleted from the cache. Display the value of the deleted cache:
The following is a code snippet: Cache. Remove ("mycachekey "); |
ASP. NET cache data tips: Notify the application when deleting a cache item It may be useful to notify the application when items are removed from the cache. For example, a report with a cache may take a lot of time to process. When the report is removed from the cache, you do not have to wait for the report to be processed the next time you request the report. ASP. NET provides cacheitemremovedcallback delegation, which can send notifications when items are removed from the cache. The cacheitemremovedreason enumeration is also provided to specify the reason for removing cache items. For example, assume that there is a reportmanager object, which has two methods: getreport and cachereport. The getreport report method checks the cache to check whether the report has been cached. If not, this method re-generates the report and caches it. The cachereport method has the same function signature as the cacheitemremovedcallback delegate. When a report is removed from the cache, ASP. NET calls the cachereport method and adds the Report to the cache again. 1) Create an ASP. NET webpage. The webpage will call the methods used in the class to add items to the cache.
The following is a code snippet: Protected void page_load (Object sender, eventargs E) { This. label1.text = reportmanager. getreport (); } |
2) create a full reportmanager class for Processing notifications when deleting items from the cache.
The following is a code snippet: Using system; Using system. Web; Using system. Web. caching; Public static class reportmanager { Private Static bool _ reportremovedfromcache = false;Static reportmanager (){} // Obtain items from the cache Public static string getreport () { Lock (typeof (reportmanager )) { If (httpcontext. Current. cache ["myreport"]! = NULL) {// The myreport cache item exists and the cache value is returned. Return (string) httpruntime. cache ["myreport"]; } Else {// If the myreport cache item does not exist, the myreport cache item is created. Cachereport (); Return (string) httpruntime. cache ["myreport"]; } } } // Add an item to the cache with the name of myreport, and set this item to expire one minute after it is added to the cache. // Register the reportremovecallback method for this method to be called when an item is deleted from the cache. Public static void cachereport () { Lock (typeof (reportmanager )) { Httpcontext. Current. cache. Add ("myreport ", Createreport (), null, datetime. maxvalue, New timespan (0, 1, 0 ), System. Web. caching. cacheitempriority. Default, Reportremovedcallback ); } } // Create a report. The value of the myreport cache item during this report Private Static string createreport () { System. Text. stringbuilder myreport = New system. Text. stringbuilder (); Myreport. append ("Sales Report <br/> "); Myreport. append ("2005 Q2 figures <br/> "); Myreport. append ("sales ne region-$2 million <br/> "); Myreport. append ("sales NW region-$4.5 million <br/> "); Myreport. append ("report generated:" + datetime. Now. tostring () + "<Br/> "); Myreport. append ("report removed from cache:" + _ Reportremovedfromcache. tostring ()); Return myreport. tostring (); } // This method is called when an item is deleted from the cache. Public static void reportremovedcallback (string key, object value, Cacheitemremovedreason removedreason) { _ Reportremovedfromcache = true; Cachereport (); } } |
Should not be in ASP. NET page, because the page may have been released before the items are deleted from the cache, so the method used to process the callback will be unavailable. net. To ensure that the method for processing callback still exists when an item is deleted from the cache, use the static class of this method. However, the disadvantage of static classes is to ensure that all static methods are thread-safe, so the lock keyword is used. |