手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >ASP.NET缓存处理类实例
ASP.NET缓存处理类实例
摘要:本文实例讲述了缓存处理类。分享给大家供大家参考。具体如下:缓存处理类。用法:Justcopythiscodeintoanewclassfil...

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

缓存处理类。

用法:

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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 using System; using System.Collections.Generic; using System.Web.Caching; using System.Web; /// summary /// 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. /// /summary 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 Dictionary 的基本用法示例介绍

ASP.NET中 Execl导出的六种方法实例

ASP.NET中母版页和shtml实例入门

在ASP.NET 中实现单点登录

ASP.NET中ServerPush用法实例分析

ASP.NET实现基于Forms认证的WebService应用实例

ASP.net 生成缩略图的实例源代码

ASP.NET 回发密码框清空问题处理方法

ASP.NET数据库编程之Access连接失败

ASP.NET抓取网页内容的实现方法

精品推荐
分类导航