Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <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;
            }
        }
Esempio n. 3
0
        /// <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);
        }