Exemple #1
0
        /// <summary>
        /// Internals the update.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="ifNotExistsThenInsert">if set to <c>true</c> [if not exists then insert].</param>
        /// <param name="getExpiredStamp">The get expired stamp.</param>
        /// <returns>
        /// System.Nullable&lt;DateTime&gt;.
        /// </returns>
        protected override DateTime?InternalUpdate(TKey key, TEntity entity, bool ifNotExistsThenInsert, Func <DateTime?> getExpiredStamp)
        {
            DateTime?result = null;

            if (key != null)
            {
                try
                {
                    lock (itemChangeLocker)
                    {
                        // NOTE: Force to remove then add, is to ensure the sequence is correct. Otherwise, the most frequency updated item would still be knick out the container due to capacity limits.
                        if (container.ContainsKey(key))
                        {
                            container.Remove(key);
                        }
                        else
                        {
                            if (!ifNotExistsThenInsert)
                            {
                                return(result);
                            }
                        }

                        result = getExpiredStamp();
                        container.Add(key, new MemoryCacheItem <TEntity> {
                            Value = entity, ExpiredStamp = result
                        });
                        InternalMaintainCapacity();
                    }
                }
                catch (Exception ex)
                {
                    throw ex.Handle(key);
                }
            }

            return(result);
        }