/// <summary> /// 更新数据 /// 不存在时不更新 /// </summary> /// <param name="item"></param> /// <returns></returns> public ICacheItem Update(ICacheItem item) { try { if (item == null) { return(null); } CheckUniqueIndex(); string uniqueKey = _uniqueIndex.GetIndexKey(item); List <ICacheItem> datas = Get(uniqueKey); if (datas.Count <= 0) { return(null); } item.Copy(datas[0]); return(datas[0]); } catch (Exception ex) { CacheLog.LogError(string.Format("更新表数据失败:{0}, 表【{1}】;StackTrace:{2}", ex.Message, TableName, ex.StackTrace != null ? ex.StackTrace : "")); throw ex; } }
/// <summary> /// 添加缓存项 /// 索引重复,会抛出异常 /// </summary> /// <param name="item"></param> /// <returns></returns> public bool Add(ICacheItem item) { string blockKey = _index.GetIndexKey(item); string uniqueKey = _uniqueIndex.GetIndexKey(item); try { _blockLock.EnterUpgradeableReadLock(); if (_uniqueBlock.ContainsKey(uniqueKey)) { throw new Exception(string.Format("索引重复,数据已存在:唯一索引:{0},区块索引:{1}", uniqueKey, blockKey)); } try { _blockLock.EnterWriteLock(); if (!_block.ContainsKey(blockKey)) { _block.Add(blockKey, new List <ICacheItem>()); } _block[blockKey].Add(item); _uniqueBlock.Add(uniqueKey, item); _datas.Add(item); CacheLog.LogDebug(string.Format("添加数据:{0}", item.ToString())); } catch (Exception ex) { throw ex; } finally { _blockLock.ExitWriteLock(); } } catch (Exception ex) { throw ex; } finally { _blockLock.ExitUpgradeableReadLock(); } return(true); }