用Cache存储数据

来源:互联网 发布:easymule 软件 编辑:程序博客网 时间:2024/05/01 12:12

public class DataCache
 {

  static DataCache()
  {
   _Instance = new DataCache();
  }

  static DataCache _Instance;

  public static DataCache Instance
  {
   get { return _Instance; }
  }
  
  public string Add(object obj)
  {
   string key = Guid.NewGuid().ToString();
   HttpContext.Current.Cache.Add( key, obj, null, DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.High, null);
   return key;
  }

  public object Get(string key, bool keep)
  {
   object obj = HttpContext.Current.Cache.Get( key );
   if( obj != null && !keep )
   {
    HttpContext.Current.Cache.Remove( key );
   }
   return obj;
  }

  public object Get( string key )
  {
   return Get( key, false );
  }
 }

原创粉丝点击