Exemple #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);
        }
Exemple #2
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);
                }
            }
        }
Exemple #3
0
 public EvictionHint CompatibleHint(EvictionHint eh)
 {
     return(_evctPolicy.CompatibleHint(eh));
 }
Exemple #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);
        }
Exemple #5
0
 public EvictionHint CompatibleHint(EvictionHint eh, PoolManager poolManager)
 {
     return(_evctPolicy.CompatibleHint(eh, poolManager));
 }