首页| C#.NET疑难解答| C#开发者博客| c#技术堂| C#知识堂| C#技术园| C#.NET资料博客| NET学习博客| ASP.NET技术应用博客

香水.net's Blog

香水.net's Blog

导航

ASP.net 针对cache技术的应用,经典的CACHE的类

Posted on 2006-10-11 15:17  香水  阅读(329)  评论(0)    收藏  举报

using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;

namespace BoovooCms.CacheService
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 public class BoovooCache
 {
  private static readonly Cache boovoo_cache;
  public static readonly int DayFactor = 17280;
  public static readonly int HourFactor = 720;
  public static readonly int MinuteFactor = 12;  
  private static int Factor = 5;

  public static void ReSetFactor(int cacheFactor)
  {
   Factor = cacheFactor;
  }
  static BoovooCache()
  {
   HttpContext context = HttpContext.Current;
   if(context != null)
   {
    boovoo_cache = context.Cache;
   }
   else
   {
    boovoo_cache = HttpRuntime.Cache;
   }
  }

//  public object this[string key]
//  {
//   get
//   {
//    return _cache[key];
//   }
//   set
//   {
//    _cache[key]=value;
//   }
//  }

  /// <summary>
  /// 清空Cache中的项
  /// </summary>
  public static void Clear()
  {
   IDictionaryEnumerator CacheEnum = boovoo_cache.GetEnumerator();
   while(CacheEnum.MoveNext()) 
   {
    boovoo_cache.Remove(CacheEnum.Key.ToString());
   }

  }
  public static void RemoveByPattern(string pattern)
  {
   IDictionaryEnumerator CacheEnum = boovoo_cache.GetEnumerator();
   Regex regex = new Regex(pattern,RegexOptions.IgnoreCase|RegexOptions.Singleline|RegexOptions.Compiled);
   while(CacheEnum.MoveNext())
   {
    if(regex.IsMatch(CacheEnum.Key.ToString()))
    {
     boovoo_cache.Remove(CacheEnum.Key.ToString());
    }
   }
  }

  /// <summary>
  /// 移除key所指定的Cache项
  /// </summary>
  /// <param name="key"></param>
  public static void Remove(string key)
  {
   boovoo_cache.Remove(key);
  }

  /// <summary>
  /// 将obj加入Cache
  /// </summary>
  /// <param name="key"></param>
  /// <param name="obj"></param>
  public static void Insert(string key, object obj)
  {
   Insert(key,obj,null,1);
  }

  public static void Insert(string key, object obj, CacheDependency dep)
  {
   Insert(key,obj,dep,HourFactor * 12);
  }

  public static void Insert(string key, object obj, int seconds)
  {
   Insert(key,obj,null,seconds);
  }

  public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
  {
   Insert(key,obj,null,seconds,priority);
  }

  public static void Insert(string key, object obj, CacheDependency dep, int seconds)
  {
   Insert(key,obj,dep,seconds,CacheItemPriority.Normal);
  }

  public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
  {
   if(obj != null)
   {
    boovoo_cache.Insert(key,obj,dep,DateTime.Now.AddSeconds(Factor * seconds), TimeSpan.Zero,priority,null);
   }

  }

  public static void MicroInsert (string key, object obj, int secondFactor)
  {
   if(obj != null)
   {
    boovoo_cache.Insert(key,obj,null,DateTime.Now.AddSeconds(Factor * secondFactor), TimeSpan.Zero);
   }
  }

  /// <summary>
  /// 将obj加入Cache使用最大缓存时间
  /// </summary>
  /// <param name="key"></param>
  /// <param name="obj"></param>
  public static void Max(string key, object obj)
  {
   Max(key,obj,null);
  }

  public static void Max(string key, object obj, CacheDependency dep)
  {
   if(obj != null)
   {
    boovoo_cache.Insert(key,obj,dep,DateTime.MaxValue,TimeSpan.Zero,CacheItemPriority.AboveNormal,null);
   }
  }

  public static object Get(string key)
  {
   return boovoo_cache[key];
  }
 }
}