Esempio n. 1
0
        /// <summary>
        /// Método interno acionado para adicionar um novo item.
        /// </summary>
        /// <param name="key">Chave do item.</param>
        /// <param name="cacheEntry">Instancia da entrada que está sendo adicionada.</param>
        /// <param name="isUserOperation">True se for uma operação do usuário.</param>
        /// <returns>Resultado da operação.</returns>
        internal override CacheAddResult AddInternal(object key, CacheEntry cacheEntry, bool isUserOperation)
        {
            if (_cacheStore == null)
            {
                throw new InvalidOperationException();
            }
            if (_evictionPolicy != null)
            {
                if (cacheEntry.EvictionHint is PriorityEvictionHint)
                {
                    cacheEntry.Priority = ((PriorityEvictionHint)cacheEntry.EvictionHint).Priority;
                }
                cacheEntry.EvictionHint = _evictionPolicy.CompatibleHint(cacheEntry.EvictionHint);
            }
            StoreAddResult result = _cacheStore.Add(key, cacheEntry);

            if ((result == StoreAddResult.Success || result == StoreAddResult.SuccessNearEviction) && (_evictionPolicy != null))
            {
                _evictionPolicy.Notify(key, null, cacheEntry.EvictionHint);
            }
            switch (result)
            {
            case StoreAddResult.Success:
                return(CacheAddResult.Success);

            case StoreAddResult.KeyExists:
                return(CacheAddResult.KeyExists);

            case StoreAddResult.NotEnoughSpace:
                return(CacheAddResult.NeedsEviction);

            case StoreAddResult.SuccessNearEviction:
                return(CacheAddResult.SuccessNearEviction);
            }
            return(CacheAddResult.Failure);
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Adds a pair of key and value to the cache. Throws an exception or reports error
        /// if the specified key already exists in the cache.
        /// </summary>
        /// <param name="key">key of the entry.</param>
        /// <param name="cacheEntry">the cache entry.</param>
        /// <returns>returns the result of operation.</returns>
        internal override CacheAddResult AddInternal(object key, CacheEntry cacheEntry, bool isUserOperation, OperationContext operationContext)
        {
            try
            {
                if (_cacheStore == null)
                {
                    throw new InvalidOperationException();
                }
                if (cacheEntry != null)
                {
                    cacheEntry.MarkInUse(NCModulesConstants.LocalCache);
                }

                if (_evictionPolicy != null)
                {
                    if (cacheEntry.EvictionHint is PriorityEvictionHint)
                    {
                        cacheEntry.Priority = ((PriorityEvictionHint)cacheEntry.EvictionHint).Priority;
                    }

                    EvictionHint oldEvictionHint = cacheEntry.EvictionHint;

                    cacheEntry.EvictionHint = _evictionPolicy.CompatibleHint(cacheEntry.EvictionHint, _context.StorePoolManager);

                    if (oldEvictionHint != null && !ReferenceEquals(oldEvictionHint, cacheEntry.EvictionHint))
                    {
                        MiscUtil.ReturnEvictionHintToPool(oldEvictionHint, _context.StorePoolManager);
                    }
                }

                // No Need to insert Eviction if Eviction is turned off it will reduce cache-entry overhead

                if (_evictionPolicy == null)
                {
                    cacheEntry.EvictionHint = null;
                }

                StoreAddResult result = _cacheStore.Add(key, cacheEntry, !isUserOperation);
                if (result == StoreAddResult.Success || result == StoreAddResult.SuccessNearEviction)
                {
                    cacheEntry.IsStored = true;
                    if (_evictionPolicy != null)
                    {
                        _evictionPolicy.Notify(key, null, cacheEntry.EvictionHint);
                    }
                }
                if (result == StoreAddResult.NotEnoughSpace && !_notifyCacheFull)
                {
                    _notifyCacheFull = true;
                    _context.NCacheLog.Error("LocalCache.AddInternal", "The cache is full and not enough items could be evicted.");
                }

                if (_context.PerfStatsColl != null)
                {
                    if (_evictionPolicy != null)
                    {
                        _context.PerfStatsColl.SetEvictionIndexSize((long)_evictionPolicy.IndexInMemorySize);
                    }

                    if (_context.ExpiryMgr != null)
                    {
                        _context.PerfStatsColl.SetExpirationIndexSize((long)_context.ExpiryMgr.IndexInMemorySize);
                    }
                }

                switch (result)
                {
                case StoreAddResult.Success: return(CacheAddResult.Success);

                case StoreAddResult.KeyExists: return(CacheAddResult.KeyExists);

                case StoreAddResult.NotEnoughSpace: return(CacheAddResult.NeedsEviction);

                case StoreAddResult.SuccessNearEviction: return(CacheAddResult.SuccessNearEviction);
                }

                return(CacheAddResult.Failure);
            }
            finally
            {
                if (cacheEntry != null)
                {
                    cacheEntry.MarkFree(NCModulesConstants.LocalCache);
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Adds a pair of key and value to the cache. Throws an exception or reports error
        /// if the specified key already exists in the cache.
        /// </summary>
        /// <param name="key">key of the entry.</param>
        /// <param name="cacheEntry">the cache entry.</param>
        /// <returns>returns the result of operation.</returns>
        internal override CacheAddResult AddInternal(object key, CacheEntry cacheEntry, bool isUserOperation, OperationContext operationContext)
        {
            if (ServerMonitor.MonitorActivity)
            {
                ServerMonitor.LogClientActivity("LocalCache.Add_1", "");
            }
            if (_cacheStore == null)
            {
                throw new InvalidOperationException();
            }

            if (_evictionPolicy != null)
            {
                if (cacheEntry.EvictionHint is PriorityEvictionHint)
                {
                    cacheEntry.Priority = ((PriorityEvictionHint)cacheEntry.EvictionHint).Priority;
                }

                cacheEntry.EvictionHint = _evictionPolicy.CompatibleHint(cacheEntry.EvictionHint);
            }

            //No Need to insert Eviction if Eviction is turned off it will reduce cache-entry overhead
            if (_evictionPolicy == null)
            {
                cacheEntry.EvictionHint = null;
            }

            StoreAddResult result = _cacheStore.Add(key, cacheEntry, !isUserOperation);

            // Operation completed!
            if (result == StoreAddResult.Success || result == StoreAddResult.SuccessNearEviction)
            {
                if (_evictionPolicy != null)
                {
                    _evictionPolicy.Notify(key, null, cacheEntry.EvictionHint);
                }
            }
            if (result == StoreAddResult.NotEnoughSpace && !_notifyCacheFull)
            {
                _notifyCacheFull = true;
                _context.NCacheLog.Error("LocalCache.AddInternal", "The cache is full and not enough items could be evicted.");
            }

            if (_context.PerfStatsColl != null)
            {
                if (_evictionPolicy != null)
                {
                    _context.PerfStatsColl.SetEvictionIndexSize((long)_evictionPolicy.IndexInMemorySize);
                }

                if (_context.ExpiryMgr != null)
                {
                    _context.PerfStatsColl.SetExpirationIndexSize((long)_context.ExpiryMgr.IndexInMemorySize);
                }
            }


            switch (result)
            {
            case StoreAddResult.Success: return(CacheAddResult.Success);

            case StoreAddResult.KeyExists: return(CacheAddResult.KeyExists);

            case StoreAddResult.NotEnoughSpace: return(CacheAddResult.NeedsEviction);

            case StoreAddResult.SuccessNearEviction: return(CacheAddResult.SuccessNearEviction);
            }
            return(CacheAddResult.Failure);
        }