首页 > 编程 > .NET > 正文

ASP.NET缓存处理类实例

2024-07-10 13:29:09
字体:
来源:转载
供稿:网友

这篇文章主要介绍了ASP.NET缓存处理类,实例分析了asp.net缓存的使用技巧,需要的朋友可以参考下

本文实例讲述了ASP.NET缓存处理类。分享给大家供大家参考。具体如下:

ASP.NET 缓存处理类。

用法:

Just copy this code into a new class file (.cs) and add it to your ASP .NET website. One thing to keep in mind is that data stored in ASP .NET Cache can be accessible across all sessions. So when creating a cacheID for the object to be stored, it must be unique (or it could be overwritten). I usually store the unique cacheID in the session and then use that to referrence the cacheID. (e.g. CacheHandler.Write(Session["MyCacheData"], myData);)

具体代码如下:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Web.Caching; 
  4. using System.Web; 
  5. /// <summary> 
  6. /// This class reads/writes to ASP .NET server cache. For the sake of  
  7. /// simplicity, the class writes objects to cache with no expirateion. 
  8. /// Use the Remove() function to programmatically remove objects stored 
  9. /// from the server cache. This class was created as an alternative to  
  10. /// storing large objects in the session. 
  11. /// </summary> 
  12. public class CacheHandler 
  13. public static bool Write(string cacheID, object data) 
  14. if (HttpContext.Current == null
  15. return false
  16. if (cacheID == null || cacheID.Equals("")) 
  17. return false
  18. HttpRuntime.Cache.Insert( 
  19. cacheID, data, null, Cache.NoAbsoluteExpiration,  
  20. Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null 
  21. ); 
  22. return true
  23. public static object Read(string cacheID) 
  24. if (HttpContext.Current == null
  25. return null
  26. return HttpRuntime.Cache.Get(cacheID); 
  27. public static void Remove(string cacheID) 
  28. if (HttpContext.Current == null ) 
  29. return
  30. if (cacheID == null || cacheID.Equals("")) 
  31. return
  32. HttpRuntime.Cache.Remove(cacheID); 

希望本文所述对大家的asp.net程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表