欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET缓存处理类实例

程序员文章站 2023-12-22 16:07:22
本文实例讲述了asp.net缓存处理类。分享给大家供大家参考。具体如下: asp.net 缓存处理类。 用法: just copy this code into a...

本文实例讲述了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程序设计有所帮助。

上一篇:

下一篇: