您现在的位置是:网站首页> 编程资料编程资料
ASP.NET缓存处理类实例_实用技巧_
2023-05-24
396人已围观
简介 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);)
具体代码如下:
using System; using System.Collections.Generic; using System.Web.Caching; using System.Web; ////// This class reads/writes to ASP .NET server cache. For the sake of /// simplicity, the class writes objects to cache with no expirateion. /// Use the Remove() function to programmatically remove objects stored /// from the server cache. This class was created as an alternative to /// storing large objects in the session. /// public class CacheHandler { public static bool Write(string cacheID, object data) { if (HttpContext.Current == null) return false; if (cacheID == null || cacheID.Equals("")) return false; HttpRuntime.Cache.Insert( cacheID, data, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null ); return true; } public static object Read(string cacheID) { if (HttpContext.Current == null) return null; return HttpRuntime.Cache.Get(cacheID); } public static void Remove(string cacheID) { if (HttpContext.Current == null ) return; if (cacheID == null || cacheID.Equals("")) return; HttpRuntime.Cache.Remove(cacheID); } }
希望本文所述对大家的asp.net程序设计有所帮助。
您可能感兴趣的文章:
相关内容
- ASP.NET创建动态缩略图的方法_实用技巧_
- 浅谈ASP.NET中MVC 4 的JS/CSS打包压缩功能_实用技巧_
- .NET逻辑分层架构总结_实用技巧_
- asp.net模板引擎Razor调用外部方法用法实例_实用技巧_
- asp.net模板引擎Razor中cacheName的问题分析_实用技巧_
- asp.net实现在非MVC中使用Razor模板引擎的方法_实用技巧_
- ASP.NET中ServerPush用法实例分析_实用技巧_
- ASP.NET中Global和URLReWrite用法_实用技巧_
- ASP.NET中Application全局对象用法实例浅析_实用技巧_
- ASP.NET中母版页和shtml实例入门_实用技巧_
