扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
一个世界上最懒惰的程序员写的Cache也能让你的复杂计算程序(数据库程序)大大提高速度
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); } } }
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。