ASP. NET cache data skills

Source: Internet
Author: User
Tags delete cache

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.

 
 
  1. StringCachedString;
  2. If(Cache ["CacheItem"]! =Null)
  3. {
  4. CachedString = (String) Cache ["CacheItem"];
  5. }
  6. Else 
  7. {
  8.  
  9. // When the cache does not exist 
  10. Cache. Insert ("CacheItem","Hello, World .")
  11. CachedString = (String) Cache ["CacheItem"];
  12. }

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:

 
 
  1. 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.

 
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     this.Label1.Text = ReportManager.GetReport();  

2) create a full ReportManager class for Processing notifications when deleting items from the cache.

 
 
  1. UsingSystem;
  2. UsingSystem. Web;
  3. UsingSystem. Web. Caching;
  4. Public Static ClassReportManager
  5. {
  6. Private Static Bool_ ReportRemovedFromCache =False;
  7. StaticReportManager (){}
  8. // Obtain items from the cache 
  9. Public StaticString GetReport ()
  10. {
  11. Lock(Typeof(ReportManager ))
  12. {
  13. If(HttpContext. Current. Cache ["MyReport"]! =Null)
  14. {// The MyReport cache item exists and the cache value is returned. 
  15. Return(String) HttpRuntime. Cache ["MyReport"];
  16. }
  17. Else 
  18. {// If the MyReport cache item does not exist, the MyReport cache item is created. 
  19. CacheReport ();
  20. Return(String) HttpRuntime. Cache ["MyReport"];
  21. }
  22. }
  23. }
  24.  
  25. // 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. 
  26. // Register the ReportRemoveCallback method for this method to be called when an item is deleted from the cache. 
  27. Public Static VoidCacheReport ()
  28. {
  29. Lock(Typeof(ReportManager ))
  30. {
  31. HttpContext. Current. Cache. Add ("MyReport",
  32. CreateReport (),Null, DateTime. MaxValue,
  33. NewTimeSpan (0, 1, 0 ),
  34. System. Web. Caching. CacheItemPriority. Default,
  35. ReportRemovedCallback );
  36. }
  37. }
  38.  
  39. // Create a report. The value of the MyReport cache item during this report 
  40. Private Static StringCreateReport ()
  41. {
  42. System. Text. StringBuilder myReport =
  43. NewSystem. Text. StringBuilder ();
  44. MyReport. Append ("Sales Report <br/>");
  45. MyReport. Append ("2005 Q2 Figures <br/>");
  46. MyReport. Append ("Sales NE Region-$2 million <br/>");
  47. MyReport. Append ("Sales NW Region-$4.5 million <br/>");
  48. MyReport. Append ("Report Generated :"+ DateTime. Now. ToString ()
  49. +"<Br/>");
  50. MyReport. Append ("Report Removed From Cache :"+
  51. _ ReportRemovedFromCache. ToString ());
  52. ReturnMyReport. ToString ();
  53. }
  54.  
  55. // This method is called when an item is deleted from the cache. 
  56. Public Static VoidReportRemovedCallback (String key,ObjectValue,
  57. CacheItemRemovedReason removedReason)
  58. {
  59. _ ReportRemovedFromCache =True;
  60. CacheReport ();
  61. }
  62. }

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.

This article is from Bodhi House: cache application data 2)

  1. Overview of ASP. NET cache data adding methods
  2. Basic concepts of ASP. NET Cache Mechanism
  3. ASP. NET cache mechanism: balance between development efficiency and Optimization
  4. . NET distributed cache for Memcached execution speed detection
  5. How to Avoid ASP. NET cache occupying system resources

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.