/// <summary> /// Provides implementation of Add method of the ICacheStorage interface. Add 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 StoreAddResult Add(object key, IStorageEntry item, Boolean allowExtendedSize) { try { if (_itemDict.ContainsKey(key)) { return(StoreAddResult.KeyExists); } StoreStatus status = HasSpace((ISizable)item, Common.MemoryUtil.GetStringSize(key), allowExtendedSize); if (status == StoreStatus.HasNotEnoughSpace) { return(StoreAddResult.NotEnoughSpace); } byte[] buffer = StoreItem.ToBinary(key, item, CacheContext); lock (_itemDict.SyncRoot) { MmfObjectPtr info = _internalStore.Add(buffer); if (info == null) { return(StoreAddResult.NotEnoughSpace); } info.View.ParentStorageProvider = this; _itemDict.Add(key, info); base.Added(item, Common.MemoryUtil.GetStringSize(key)); } if (status == StoreStatus.NearEviction) { return(StoreAddResult.SuccessNearEviction); } } catch (OutOfMemoryException e) { Trace.error("OutofMemoryException::MmfStorageProvider.Add()", e.ToString()); return(StoreAddResult.NotEnoughSpace); } catch (Exception e) { Trace.error("General Exception::MmfStorageProvider.Add()", e.ToString()); return(StoreAddResult.Failure); } return(StoreAddResult.Success); }
/// <summary> /// Save store state and data to persistent medium. /// </summary> void IPersistentCacheStorage.SaveStorageState() { try { if (_stateChangeId == 0) { return; } string fileName = Path.Combine(_internalStore.RootDir, STATE_FILENAME); lock (_itemDict) { using (Stream state = new FileStream(fileName, FileMode.Create)) { using (BinaryWriter writer = new BinaryWriter(state)) { // Write count of total items in store. writer.Write(_itemDict.Count); IDictionaryEnumerator i = _itemDict.GetEnumerator(); for (; i.MoveNext();) { try { byte[] buffer = StoreItem.ToBinary(i.Key, i.Value, CacheContext); writer.Write(buffer.Length); writer.Write(buffer); } catch (Exception) { // Indicate failure by writing -1. writer.Write((int)-1); } } } } } } catch (Exception e) { Trace.error("FileSystemStorageProvider.SaveStorageState()", e.ToString()); } finally { ResetStateChanged(); } }
/// <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); } }