自己实现的一个 .net 缓存类
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; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。