Exemple #1
0
        /// <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>
        /// Insere um novo item.
        /// </summary>
        /// <param name="key">Chave do item.</param>
        /// <param name="item">Instancia do item.</param>
        /// <returns>Resultado da operação.</returns>
        public override StoreInsResult Insert(object key, object item)
        {
            StoreInsResult notEnoughSpace;

            try
            {
                MmfObjectPtr info = (MmfObjectPtr)_itemDict[key];
                object       obj2 = null;
                if (info == null)
                {
                    switch (this.Add(key, item))
                    {
                    case StoreAddResult.NotEnoughSpace:
                        return(StoreInsResult.NotEnoughSpace);

                    case StoreAddResult.Failure:
                        return(StoreInsResult.Failure);
                    }
                    return(StoreInsResult.Success);
                }
                obj2 = this.Get(key);
                StorageProviderBase.StoreStatus status = base.HasSpace(obj2 as ISizable, (ISizable)item);
                if (status == StorageProviderBase.StoreStatus.HasNotEnoughSpace)
                {
                    notEnoughSpace = StoreInsResult.NotEnoughSpace;
                }
                else
                {
                    byte[] buffer = StoreItem.ToBinary(key, item, base.CacheContext);
                    lock (_itemDict.SyncRoot)
                    {
                        MmfObjectPtr ptr2 = _internalStore.Insert(info, buffer);
                        if (ptr2 == null)
                        {
                            return(StoreInsResult.NotEnoughSpace);
                        }
                        if (ptr2.Area != info.Area)
                        {
                            _itemDict[key] = ptr2;
                            _internalStore.Remove(info);
                        }
                        base.Inserted(obj2 as ISizable, item as ISizable);
                        if (status == StorageProviderBase.StoreStatus.NearEviction)
                        {
                            return((obj2 != null) ? StoreInsResult.SuccessOverwriteNearEviction : StoreInsResult.SuccessNearEviction);
                        }
                        notEnoughSpace = (ptr2 != null) ? StoreInsResult.SuccessOverwrite : StoreInsResult.Success;
                    }
                }
            }
            catch (OutOfMemoryException exception)
            {
                Trace.Error("MmfStorageProvider.Insert()".GetFormatter(), exception.GetFormatter());
                notEnoughSpace = StoreInsResult.NotEnoughSpace;
            }
            catch (Exception exception2)
            {
                Trace.Error("MmfStorageProvider.Insert()".GetFormatter(), exception2.GetFormatter());
                notEnoughSpace = StoreInsResult.Failure;
            }
            return(notEnoughSpace);
        }