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;
/// <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中执行控件(如ImageButton、LinkButton等)命令不刷新页面
在操作时,很多时候希望提交命令之后不刷新页面:1、防止页面拉动,2、防止主框架中刷新丢失当前运行的子界面。2009-03-03
ASP.NET Core自定义本地化教程之从文本文件读取本地化字符串
使用 ASP.NET Core 创建多语言网站,可让网站拥有更多受众。下面这篇文章主要给大家介绍了关于ASP.NET Core自定义本地化教程之从文本文件读取本地化字符串的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下2018-09-09


最新评论