public T GetItem(object key) { if (_Items == null) { InitItemTable(); } CacheObjectItem <T> obj = null; if (_Items.ContainsKey(key)) { try { obj = (CacheObjectItem <T>)_Items[key]; if (!obj.IsExpired) { // 如果每次自动刷新时间 if (AutoRefreshWhenRead) { obj.Refresh(); } return(obj.GetValue()); } // item已过期,删除 lock (_Items.SyncRoot) { _Items.Remove(key); } } catch { } } return(default(T)); }
public void SetItem(object key, T obj) { if (key == null || obj == null) { return; } if (_Items == null) { InitItemTable(); } CacheObjectItem <T> objItem = CacheObjectItem <T> .GetObjectItem(obj, _ExpireSeconds); objItem.Refresh(); _Items[key] = objItem; }
public List <T> GetAllItems() { if (_Items == null) { InitItemTable(); } List <T> result = null; if (_Items.Count < 1) { return(result); } try { lock (_Items.SyncRoot) { if (_Items.Count < 1) { return(result); } result = new List <T>(_Items.Count); CacheObjectItem <T> cacheItem = null; foreach (DictionaryEntry item in _Items) { cacheItem = (CacheObjectItem <T>)item.Value; result.Add(cacheItem.GetValue()); } } } catch { } finally { } return(result); }