using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Mesnac.Basic { /// /// 全局缓存工厂类 /// public class CacheFactory { #region 单例实现 private static CacheFactory _instance = null; //静态成员 /// /// 私有构造方法 /// private CacheFactory() { } /// /// 缓存工厂静态实例 /// public static CacheFactory Instance { get { lock(String.Empty) { if (_instance == null) { _instance = new CacheFactory(); } return _instance; } } } #endregion #region 字段定义 private Dictionary dic = new Dictionary(); #endregion #region 方法定义 /// /// 判断缓存工厂中是否存在指定的缓存项 /// /// 缓存的key值 /// 存在返回true,不存在返回false public bool ContainsKey(string key) { return this.dic.ContainsKey(key); } /// /// 缓存方法 /// /// 缓存对象的类型 /// 缓存的key值 /// 缓存的对象值 public void Cache(string key, T value) { this.dic[key] = value; } /// /// 从缓存中获取对象 /// /// 缓存对象的类型 /// 缓存的key值 /// 返回缓存对象值 public T Get(string key) { if (this.dic.ContainsKey(key)) { object value = this.dic[key]; return (T)value; } return default(T); } /// /// 清空指定的缓存对象 /// /// 缓存的key值 public void ClearByKey(string key) { if (this.dic.ContainsKey(key)) { this.dic.Remove(key); } } /// /// 清空所有缓存对象 /// public void ClearAll() { this.dic.Clear(); } #endregion } }