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.
- StringCachedString;
- 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:
- 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.
- 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.
- UsingSystem;
- UsingSystem. Web;
- UsingSystem. Web. Caching;
- Public Static ClassReportManager
- {
- Private Static Bool_ ReportRemovedFromCache =False;
- StaticReportManager (){}
- // Obtain items from the cache
- Public StaticString 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 VoidCacheReport ()
- {
- Lock(Typeof(ReportManager ))
- {
- HttpContext. Current. Cache. Add ("MyReport",
- CreateReport (),Null, DateTime. MaxValue,
- NewTimeSpan (0, 1, 0 ),
- System. Web. Caching. CacheItemPriority. Default,
- ReportRemovedCallback );
- }
- }
-
- // Create a report. The value of the MyReport cache item during this report
- Private Static StringCreateReport ()
- {
- System. Text. StringBuilder myReport =
- NewSystem. 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 ());
- ReturnMyReport. ToString ();
- }
-
- // This method is called when an item is deleted from the cache.
- Public Static VoidReportRemovedCallback (String key,ObjectValue,
- 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.
This article is from Bodhi House: cache application data 2)
- Overview of ASP. NET cache data adding methods
- Basic concepts of ASP. NET Cache Mechanism
- ASP. NET cache mechanism: balance between development efficiency and Optimization
- . NET distributed cache for Memcached execution speed detection
- How to Avoid ASP. NET cache occupying system resources