/// <summary> /// Load store state and data from persistent medium. /// </summary> void IPersistentCacheStorage.LoadStorageState() { try { lock (_itemDict.SyncRoot) { _itemDict.Clear(); for (IEnumerator i = _internalStore.GetEnumerator(); i.MoveNext();) { MemArena arena = (MemArena)i.Current; if (!arena.IsFree) { byte[] data = arena.GetMemContents(); StoreItem item = StoreItem.FromBinary(data, CacheContext); _itemDict.Add(item.Key, new MmfObjectPtr(arena.View, arena)); } } } } catch (Exception e) { Trace.error("MmfStorageProvider.IPersistentCacheStorage()", e.ToString()); } }
/// <summary> /// Provides implementation of Remove method of the ICacheStorage interface. /// Removes an object from the store, specified by the passed in key /// </summary> /// <param name="key">key</param> /// <returns>object</returns> public override object Remove(object key) { try { lock (_itemDict.SyncRoot) { MmfObjectPtr info = (MmfObjectPtr)_itemDict[key]; if (info != null) { byte[] data = _internalStore.Remove(info); StoreItem item = StoreItem.FromBinary(data, CacheContext); _itemDict.Remove(key); IStorageEntry strEntry = item.Value as IStorageEntry; base.Removed(strEntry, Common.MemoryUtil.GetStringSize(key), strEntry.Type); return(item.Value); } } } catch (Exception e) { Trace.error("MmfStorageProvider.Remove()", e.ToString()); } return(null); }
/// <summary> /// Load store state and data from persistent medium. /// </summary> void IPersistentCacheStorage.LoadStorageState() { try { string fileName = Path.Combine(_internalStore.RootDir, STATE_FILENAME); lock (_itemDict) { using (Stream state = new FileStream(fileName, FileMode.Open)) { using (BinaryReader reader = new BinaryReader(state)) { // Read count of total items. int count = reader.ReadInt32(); // Clear current state; if any _itemDict.Clear(); if (count < 1) { return; } for (int i = 0; i < count; i++) { int datalen = reader.ReadInt32(); // -1 means the item was not written correctly if (datalen < 0) { continue; } try { byte[] buffer = reader.ReadBytes(datalen); StoreItem item = StoreItem.FromBinary(buffer, CacheContext); // key was successfuly serialized if (_internalStore.Contains(item.Value)) { _itemDict.Add(item.Key, item.Value); } } catch (Exception) { } } } } } } catch (Exception e) { Trace.error("FileSystemStorageProvider.LoadStorageState()", e.ToString()); } finally { ResetStateChanged(); } }
/// <summary> /// Get the size of item stored in cache, specified by the passed in key /// </summary> /// <param name="key">key</param> /// <returns>item size</returns> public override int GetItemSize(object key) { try { MmfObjectPtr info = (MmfObjectPtr)_itemDict[key]; if (info != null) { byte[] data = _internalStore.Get(info); StoreItem item = StoreItem.FromBinary(data, CacheContext); return(((ISizable)item.Value).InMemorySize); } } catch (Exception e) { Trace.error("MmfStorageProvider.GetItemSize()", e.ToString()); } return(0); }