/// <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> /// Provides implementation of Insert method of the ICacheStorage interface. /// Insert/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 StoreInsResult Insert(object key, IStorageEntry item, Boolean allowExtendedSize) { Sync.AcquireWriterLock(Timeout.Infinite); try { return(_storage.Insert(key, item, allowExtendedSize)); } finally { Sync.ReleaseWriterLock(); } }
/// <summary> /// Increments the data size in cache, after item is inserted /// </summary> /// <param name="oldItem">old item</param> /// <param name="newItem">new item to be inserted</param> protected void Inserted(IStorageEntry oldItem, IStorageEntry newItem, long keySize) { if (newItem.Type != EntryType.CacheItem) { _dataSize += newItem.InMemorySize - (oldItem == null ? -keySize : oldItem.OldInMemorySize); newItem.OldInMemorySize = newItem.InMemorySize; } else { _dataSize += newItem.InMemorySize - (oldItem == null ? -keySize : oldItem.InMemorySize); } }
/// <summary> /// Handles the request /// </summary> /// <returns>The awaitable task.</returns> /// <param name="context">The requests context.</param> public async Task <bool> HandleAsync(IHttpContext context) { if (m_xsrf_storage == null) { m_xsrf_storage = await context.Storage.GetStorageAsync(XSRF_MODULE_NAME, string.Empty, -1, true); } if (m_cookie_storage == null) { m_cookie_storage = await context.Storage.GetStorageAsync(COOKIE_MODULE_NAME, string.Empty, -1, true); } return(false); }
/// <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> /// 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) { IStorageEntry e = (IStorageEntry)Get(key); if (e != null) { lock (_itemDict.SyncRoot) { _itemDict.Remove(key); base.Removed(e, Common.MemoryUtil.GetStringSize(key), e.Type); e?.MarkInUse(NCModulesConstants.Global); e?.MarkFree(NCModulesConstants.CacheStore); } } return(e); }
/// <summary> /// Provides implementation of Insert method of the ICacheStorage interface. /// Insert/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 StoreInsResult Insert(object key, IStorageEntry item, Boolean allowExtendedSize) { try { //if (ServerMonitor.MonitorActivity) ServerMonitor.LogClientActivity("Store.Insert", ""); IStorageEntry oldItem = (IStorageEntry)_itemDict[key]; StoreStatus status = HasSpace(oldItem as ISizable, (ISizable)item, Common.MemoryUtil.GetStringSize(key), allowExtendedSize); if (_evictionEnabled) { CheckIfCacheNearEviction(); } if (status == StoreStatus.HasNotEnoughSpace) { return(StoreInsResult.NotEnoughSpace); } lock (_itemDict.SyncRoot) { _itemDict[key] = item; base.Inserted(oldItem, item, Common.MemoryUtil.GetStringSize(key)); } if (status == StoreStatus.NearEviction) { //the store is almost full, need to evict. return(oldItem != null ? StoreInsResult.SuccessOverwriteNearEviction : StoreInsResult.SuccessNearEviction); } if (item != null) { item.MarkInUse(NCModulesConstants.CacheStore); } return(oldItem != null ? StoreInsResult.SuccessOverwrite : StoreInsResult.Success); } catch (OutOfMemoryException e) { return(StoreInsResult.NotEnoughSpace); } catch (Exception e) { throw e; } }
/// <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 (ServerMonitor.MonitorActivity) ServerMonitor.LogClientActivity("Store.Add", ""); lock (_itemDict.SyncRoot) { if (_itemDict.ContainsKey(key)) { return(StoreAddResult.KeyExists); } } StoreStatus status = HasSpace((ISizable)item, Common.MemoryUtil.GetStringSize(key), allowExtendedSize); CheckIfCacheNearEviction(); if (status == StoreStatus.HasNotEnoughSpace) { return(StoreAddResult.NotEnoughSpace); } lock (_itemDict.SyncRoot) { _itemDict.Add(key, item); base.Added(item, Common.MemoryUtil.GetStringSize(key)); item.MarkInUse(NCModulesConstants.CacheStore); } if (status == StoreStatus.NearEviction) { return(StoreAddResult.SuccessNearEviction); } } catch (OutOfMemoryException e) { //Trace.error("ClrHeapStorageProvider.Add()", e.ToString()); return(StoreAddResult.NotEnoughSpace); } catch (Exception e) { //Trace.error("ClrHeapStorageProvider.Add()", e.ToString()); //return StoreAddResult.Failure; throw e; } return(StoreAddResult.Success); }
/// <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) { lock (_itemDict) { IStorageEntry e = (IStorageEntry)Get(key); if (e != null) { _internalStore.Remove(_itemDict[key]); base.Removed(e, Common.MemoryUtil.GetStringSize(key), e.Type); _itemDict.Remove(key); SetStateChanged(); } return(e); } }
/// <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); } lock (_itemDict) { object value = _internalStore.Add(key, item, CacheContext); if (value != null) { _itemDict.Add(key, value); SetStateChanged(); base.Added(item, Common.MemoryUtil.GetStringSize(key)); } } if (status == StoreStatus.NearEviction) { return(StoreAddResult.SuccessNearEviction); } } catch (OutOfMemoryException e) { Trace.error("FileSystemStorageProvider.Add()", e.ToString()); return(StoreAddResult.NotEnoughSpace); } catch (Exception e) { Trace.error("FileSystemStorageProvider.Add()", e.ToString()); return(StoreAddResult.Failure); } return(StoreAddResult.Success); }
/// <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 { IStorageEntry oldItem = (IStorageEntry)Get(key); StoreStatus status = HasSpace((ISizable)item, Common.MemoryUtil.GetStringSize(key), allowExtendedSize); if (status == StoreStatus.HasNotEnoughSpace) { return(StoreInsResult.NotEnoughSpace); } lock (_itemDict) { object value = _internalStore.Insert(_itemDict[key], item, CacheContext); if (value == null) { return(StoreInsResult.Failure); } _itemDict[key] = value; SetStateChanged(); base.Inserted(oldItem, item, Common.MemoryUtil.GetStringSize(key)); } if (status == StoreStatus.NearEviction) { return(oldItem != null ? StoreInsResult.SuccessOverwriteNearEviction : StoreInsResult.SuccessNearEviction); } return(oldItem != null ? StoreInsResult.SuccessOverwrite : StoreInsResult.Success); } catch (OutOfMemoryException e) { Trace.error("FileSystemStorageProvider.Insert()", e.ToString()); return(StoreInsResult.NotEnoughSpace); } catch (Exception e) { Trace.error("FileSystemStorageProvider.Insert()", e.ToString()); return(StoreInsResult.Failure); } }
/// <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> /// Increments the data size in cache, after item is Added /// </summary> /// <param name="itemSize">item added</param> protected void Added(IStorageEntry item, long keySize) { _dataSize += (item.InMemorySize + keySize); }
/// <summary> /// Insert the key value pair to the store. Must be implemented by cache stores. /// </summary> /// <param name="key">key</param> /// <param name="item">object</param> /// <returns>returns the result of operation.</returns> public virtual StoreInsResult Insert(object key, IStorageEntry item, Boolean allowExtendedSize) { return(StoreInsResult.Failure); }
/// <summary> /// Gets or creates a storage module with the given name /// </summary> /// <returns>The storage module or null.</returns> /// <param name="name">The name of the module to get.</param> /// <param name="key">The session key of the module, or null.</param> /// <param name="ttl">The module time-to-live, zero or less means no expiration.</param> /// <param name="autocreate">Automatically create storage if not found</param> public async Task <IStorageEntry> GetStorageAsync(string name, string key, int ttl, bool autocreate) { // Copy ref to avoid race when setting a new creator var ec = ExternalCreator; IStorageEntry res = null; key = key ?? string.Empty; using (await m_lock.LockAsync()) { Dictionary <string, IStorageEntry> dict; if (!m_storage.TryGetValue(name, out dict)) { dict = m_storage[name] = new Dictionary <string, IStorageEntry>(); } if (!dict.TryGetValue(key, out res)) { if (!autocreate) { return(null); } if (ec != null) { res = await ec(name, key, ttl); } if (res == null) { res = new MemoryStorageEntry(name); } dict[key] = res; m_createcount++; } else if (res.Expires.Ticks > 0 && DateTime.Now > res.Expires) { dict.Remove(key); if (!autocreate) { m_createcount--; return(null); } res = null; if (ec != null) { res = await ec(name, key, ttl); } if (res == null) { res = new MemoryStorageEntry(name); } dict[key] = res; } if (ttl > 0) { res.Expires = DateTime.Now.AddSeconds(ttl); } if (m_createcount > HighWaterMark) { m_expirationHandler.RunNow(); } return(res); } }