using System; using System.Collections.Generic; namespace DomainBase { public class ObjectCache { //Dictionary<K,T> 会自动维护一个空链表来保存不用的单元。 //这里,使用被缓存对象的“弱引用”,允许这些对象被垃圾回收。 private Dictionary<string, WeakReference> Buffer = new Dictionary<string, WeakReference>(); public object this[string key] { get { WeakReference ret; if (Buffer.TryGetValue(key, out ret) && ret.IsAlive) return ret.Target; else return null; } set { WeakReference ret; if (Buffer.TryGetValue(key, out ret)) ret.Target = value; else Buffer.Add(key, new WeakReference(value)); } } public void Remove(string key) { Buffer.Remove(key); } } }
userdata udata=null; if (udata==null) { if(cache.userdata==null) { cache.userdata=new userdata(nowuserid); } udata=cache.userdata; } return udata.name;
userdata udata=null; while (udata == null) { if(cache.userdata==null) { cache.userdata=new userdata(nowuserid); } udata=cache.userdata; } return udata.name;
2
using System; using System.Collections.Generic; using System.Collections; using System.Text; using System.Web; using System.Web.Caching; using System.Data; namespace test { /// <summary> /// M缓存类 /// </summary> public class CommonCache { #region 构造函数 /// <summary> /// 静态构造函数 /// </summary> static CommonCache() { } #endregion #region 属性定义 /// <summary> /// 失效时间单位为分钟 /// </summary> public static int LoseTime { set { mLoseTime = value; } } #endregion #region 公共方法 /// <summary> /// 获取值 /// </summary> /// <param name="pKey">键值名</param> /// <returns>返回键值</returns> public static object GetValue(object pKey) { object retValue = null; lock (mCache.SyncRoot) { try { if (mCache.ContainsKey(pKey)) { retValue=mCache[pKey]; IList<object> valueArr = (IList<object>)retValue; DateTime dt = Convert.ToDateTime(valueArr[0]); if (dt.CompareTo(DateTime.Now) < 0) { retValue = null; } else { retValue = valueArr[1]; } } else { retValue = null; } } catch (Exception e) { retValue = null; } } return retValue; } /// <summary> /// 设置值 /// </summary> /// <param name="pKey">键值名</param> /// <param name="pValue">键值</param> public static void SetValue(object pKey, object pValue) { IList<object> valueArr = new List<object>() ; valueArr.Add (DateTime.Now.AddMinutes(mLoseTime)); valueArr.Add ( pValue); lock (mCache.SyncRoot) { try { if (!mCache.ContainsKey(pKey)) { mCache.Add(pKey, valueArr); } else { mCache[pKey] = valueArr; } } catch (Exception e) { } } } #endregion #region 字段定义 /// <summary> /// 缓存对象 /// </summary> private static Hashtable mCache = new Hashtable(); /// <summary> /// 失效时间(单位:分钟) /// </summary> private static int mLoseTime = 60; #endregion } }
using System; using System.Collections.Generic; using System.Collections; using System.Text; using System.Web; using System.Web.Caching; using System.Data; namespace test { /// <summary> /// M缓存类 /// </summary> public class CommonCache { #region 构造函数 /// <summary> /// 静态构造函数 /// </summary> static CommonCache() { } #endregion #region 属性定义 /// <summary> /// 失效时间单位为分钟 /// </summary> public static int LoseTime { set { mLoseTime = value; } } #endregion #region 公共方法 /// <summary> /// 获取值 /// </summary> /// <param name="pKey">键值名</param> /// <returns>返回键值</returns> public static object GetValue(object pKey) { object retValue = null; lock (mCache.SyncRoot) { try { if (mCache.ContainsKey(pKey)) { retValue=mCache[pKey]; IList<object> valueArr = (IList<object>)retValue; DateTime dt = Convert.ToDateTime(valueArr[0]); if (dt.CompareTo(DateTime.Now) < 0) { retValue = null; } else { retValue = valueArr[1]; } } else { retValue = null; } } catch (Exception e) { retValue = null; } } return retValue; } /// <summary> /// 设置值 /// </summary> /// <param name="pKey">键值名</param> /// <param name="pValue">键值</param> public static void SetValue(object pKey, object pValue) { IList<object> valueArr = new List<object>() ; valueArr.Add (DateTime.Now.AddMinutes(mLoseTime)); valueArr.Add ( pValue); lock (mCache.SyncRoot) { try { if (!mCache.ContainsKey(pKey)) { mCache.Add(pKey, valueArr); } else { mCache[pKey] = valueArr; } } catch (Exception e) { } } } #endregion #region 字段定义 /// <summary> /// 缓存对象 /// </summary> private static Hashtable mCache = new Hashtable(); /// <summary> /// 失效时间(单位:分钟) /// </summary>