/// <summary> /// Provides implementation of Insert method of the ICacheStorage interface. Insert /// the key value pair to the store. /// </summary> /// <param name="key">key</param> /// <param name="item">object</param> /// <returns>returns the result of operation.</returns> public override StoreInsResult Insert(object key, IStorageEntry item, Boolean allowExtendedSize) { try { MmfObjectPtr info = (MmfObjectPtr)_itemDict[key]; IStorageEntry oldItem = null; if (info == null) { StoreAddResult res = Add(key, item, allowExtendedSize); switch (res) { case StoreAddResult.NotEnoughSpace: return(StoreInsResult.NotEnoughSpace); case StoreAddResult.Failure: return(StoreInsResult.Failure); } return(StoreInsResult.Success); } oldItem = (IStorageEntry)Get(key); StoreStatus status = HasSpace(oldItem as ISizable, (ISizable)item, Common.MemoryUtil.GetStringSize(key), allowExtendedSize); if (status == StoreStatus.HasNotEnoughSpace) { return(StoreInsResult.NotEnoughSpace); } byte[] buffer = StoreItem.ToBinary(key, item, CacheContext); lock (_itemDict.SyncRoot) { MmfObjectPtr newInfo = _internalStore.Insert(info, buffer); if (newInfo == null) { return(StoreInsResult.NotEnoughSpace); } else { if (newInfo.Arena != info.Arena) { _itemDict[key] = newInfo; _internalStore.Remove(info); } base.Inserted(oldItem, item, Common.MemoryUtil.GetStringSize(key)); } if (status == StoreStatus.NearEviction) { return(oldItem != null ? StoreInsResult.SuccessOverwriteNearEviction : StoreInsResult.SuccessNearEviction); } return(newInfo != null ? StoreInsResult.SuccessOverwrite : StoreInsResult.Success); } } catch (OutOfMemoryException e) { Trace.error("MmfStorageProvider.Insert()", e.ToString()); return(StoreInsResult.NotEnoughSpace); } catch (Exception e) { Trace.error("MmfStorageProvider.Insert()", e.ToString()); return(StoreInsResult.Failure); } }
/// <summary> /// Convert a key-value pair to binary form. /// </summary> static public byte[] ToBinary(object key, object val, string cacheContext) { StoreItem item = new StoreItem(key, val); return(CompactBinaryFormatter.ToByteBuffer(item, cacheContext)); }