ASP.NET缓存处理类实例

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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缓存处理类实例全部内容,希望文章能够帮你解决ASP.NET缓存处理类实例所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。