ASP.NET中怎么实现一个缓存处理类

发布时间:2021-07-15 16:08:20 作者:Leah
来源:亿速云 阅读:103

本篇文章给大家分享的是有关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中怎么实现一个缓存处理类,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. php 缓存工具类 实现网页缓存
  2. nodejs如何使用redis作为缓存介质实现封装缓存类

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net

上一篇:asp.net中怎么利用Table生成Excel表格

下一篇:asp.net中怎么计算每个页面的执行时间

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》