public class CacheContainer { private static Hashtable ht = new Hashtable(); /// <summary> /// objId :缓存id,objObject:缓存对象,expires:绝对过期时间 /// </summary> /// <param name="objId"></param> /// <param name="objObject"></param> /// <param name="expires"></param> public void AddObject(string objId, object objObject, DateTime expires) { ContainerItem item = new ContainerItem() { Expires = expires, ObjObject = objObject }; ht[objId] = item; } /// <summary> /// 默认缓存20分钟 /// </summary> /// <param name="objId"></param> /// <param name="objObject"></param> public void AddObject(string objId, object objObject) { AddObject(objId, objObject, DateTime.Now.AddMinutes(20)); } public object GetCacheObj(string objId) { object result = null; if (ht.Contains(objId)) { var item = (ContainerItem)ht[objId]; if (DateTime.Now<item.Expires) { result =item.ObjObject; } else { ht.Remove(objId); } } return result; } /// <summary> /// objId存在则删除,否则不处理 /// </summary> /// <param name="objId"></param> public void RemoveObject(string objId) { if (ht.Contains(objId)) { ht.Remove(objId); } } } public class ContainerItem { public object ObjObject { get; set; } public DateTime Expires { get; set; } }