建站经验 experience
当前位置:首页 > 网站建设专题 > 建站经验
ASP.NET缓存处理类实例
发布日期:2015-06-19 阅读次数:903 字体大小:

这篇文章主要介绍了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);)

具体代码如下:

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);
  }
}
转载于:http://www.jb51.net/article/67920.htm

镭拓科技专注于广州网站建设、广州网站制作、广州网站设计、广州网页设计、企业网站建设、展示型网站建设、外贸网站建设的广州网站建设公司,服务热线:020-82562426。