In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the ASP.NET data caching skills". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the ASP.NET data caching skills?"
ASP.NET caching data tips: accessing cached values
Since the information stored in the cache is volatile, that is, it may be removed by ASP.NET, it is recommended that you first determine whether the item is in the cache. If not, you should add it back to the cache and retrieve the item.
String cachedString; if (Cache ["CacheItem"]! = null) {cachedString = (string) Cache ["CacheItem"];} Cache.Insert when else {/ / cache does not exist ("CacheItem", "Hello, World.") CachedString = (string) Cache ["CacheItem"];}
ASP.NET caching data tips: delete cache entries
Data in the cache may be automatically removed for any of the following reasons: the cache is full, the item has expired, and the dependency has 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. Displays the value of the delete cache:
Cache.Remove ("MyCacheKey")
ASP.NET caching data tips: notify applications when cached items are deleted
It can be useful to notify the application when items are removed from the cache. For example, you might have a cached report that takes a lot of time to create. When the report is removed from the cache, you want to regenerate the report and immediately place it in the cache so that the user does not have to wait for the report to be processed the next time the report is requested.
ASP.NET provides a CacheItemRemovedCallback delegate that can notify when items are removed from the cache. The CacheItemRemovedReason enumeration is also provided to specify the reason for removing the cached item. Example: suppose you have a ReportManager object that has two methods, GetReport and CacheReport. The GetReport report method checks the cache to see if the report is cached; if not, it regenerates the report and caches it. The CacheReport method has the same function signature as the CacheItemRemovedCallback delegate; when the report is removed from the cache, ASP.NET calls the CacheReport method and then adds the report back to the cache.
1) create an ASP.NET Web page that invokes the method in the class used to add items to the cache.
Protected void Page_Load (object sender, EventArgs e) {this.Label1.Text = ReportManager.GetReport ();}
2) create a complete class ReportManager for processing notifications when items are deleted from the cache.
Using System; using System.Web; using System.Web.Caching; public static class ReportManager {private static bool _ reportRemovedFromCache = false Static ReportManager () {} / / get the item public static String GetReport () {lock (typeof (ReportManager)) {if (HttpContext.Current.Cache ["MyReport"]! = null) {/ / exist the MyReport cache entry and return the cache value return (string) HttpRuntime.Cache ["MyReport"] } else {/ / MyReport cache entry does not exist, create MyReport cache entry CacheReport (); return (string) HttpRuntime.Cache ["MyReport"] } / / add the item to the cache with the name MyReport and set it to expire one minute after it is added to the cache. / and this method registers the ReportRemoveCallback 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 with the value of the MyReport cache entry 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.50 million
< br />"); myReport.Append (" Report Generated: "+ DateTime.Now.ToString () +"
< br />"); myReport.Append (" ReportRemoved FromCache: "+ _ reportRemovedFromCache.ToString ()); return myReport.ToString ();} / / this method is called when items are deleted from the cache. Public static void ReportRemovedCallback (String key, object value, CacheItemRemovedReason removedReason) {_ reportRemovedFromCache = true; CacheReport ();}}
Callback handlers should not be implemented in ASP.NET pages, because the page may have been freed before items are deleted from the cache, so the methods used to handle callbacks will not be available and should be implemented in non-ASP.NET assemblies. To ensure that a method for handling callbacks still exists when items are deleted from the cache, use the static class of that method. However, the disadvantage of static classes is that you need to ensure that all static methods are thread-safe, so use the lock keyword.
Thank you for your reading, these are the contents of "what are the ASP.NET data caching skills?" after the study of this article, I believe you have a deeper understanding of what ASP.NET data caching skills have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.