ASP.NET缓存处理类实例
2015-06-16 10:47
661 查看
本文实例讲述了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; /// <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程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- 星外ASP.Net的安全设置相关说明
- C#、ASP.NET通用扩展工具类之TypeParse
- Enterprise Library for .NET Framework 2.0缓存使用实例
- PowerShell中编程清空IE缓存方法
- PowerShell中使用.NET将程序集加入全局程序集缓存
- C#、ASP.NET通用扩展工具类之LogicSugar
- C#、ASP.NET通用工具类IsWhat?(可以判断数字、身份证、数据类型等等)
- C#中缓存的基本用法总结
- 程序中常用的种代码
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- ASP.NET页面间的传值的几种方法
- wap开发中如何有效的利用缓存减少消息的传送量
- 入侵ASP.net网站的经验附利用代码
- smarty缓存用法分析
- asp.net OleDbCommand 的用法
- jquery调用asp.net 页面后台的实现代码
- asp.net中的check与uncheck关键字用法解析
- ASP.NET MVC 4 捆绑和缩小实例介绍
- ASP.Net Post方式获取数据流的一种简单写法