示例#1
0
        /// <summary>
        /// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
        /// <returns>
        ///   <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            CacheBlockSet <TKey, TValue> other = (CacheBlockSet <TKey, TValue>)obj;

            return(this.Blocks.Equals(other.Blocks) && this.BlockSetCapacity.Equals(other.BlockSetCapacity));
        }
        /// <summary>
        /// Inserts key-value into full cache.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        private void InsertIntoFullCache(TKey key, TValue value)
        {
            if (BlockSetsLookup.ContainsKey(key))
            {
                CacheBlockSet <TKey, TValue> blockSet = BlockSetsLookup[key];

                if (blockSet != null)
                {
                    blockSet.Add(key, value);
                }
            }
            else
            {
                CacheBlockSet <TKey, TValue> insertableBlockSet = discardBlock(BlockSetsLookup);
                AddBlockSetToCache(key, value, insertableBlockSet);
            }
        }
        /// <summary>
        /// Returns the value and also a flag indicating if given key is present or not and a default value if not key not found
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public bool TryGetValue(TKey key, out TValue value)
        {
            value = default(TValue);

            lock (this.BlockSetsLookup)
            {
                CacheBlockSet <TKey, TValue> node = null;

                CacheBlock <TKey, TValue> mblock = null;

                foreach (var set in BlockSetsLookup.Values)
                {
                    foreach (var block in set.Blocks)
                    {
                        if (block.Key.Equals(key))
                        {
                            node = set;

                            mblock = block.Value;
                            break;
                        }
                    }
                }

                if (mblock != null)
                {
                    TValue blockvalue = default(TValue);

                    if (node.TryGetValue(key, out blockvalue))
                    {
                        node.TryGetValue(key, out blockvalue);

                        value = blockvalue;

                        var lrublock = this.lruList.Find(mblock);

                        this.lruList.Remove(lrublock);
                        this.lruList.AddLast(lrublock);
                        return(true);
                    }
                }

                value = default(TValue);
                return(false);
            }
        }
        /// <summary>
        /// Adds the block set to cache.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="blockSet">The block set.</param>
        private void AddBlockSetToCache(TKey key, TValue value, CacheBlockSet <TKey, TValue> blockSet)
        {
            CacheBlock <TKey, TValue> cacheItem = null;

            if (blockSet != null)
            {
                cacheItem = blockSet.Add(key, value);
            }

            var blocksets = BlockSetsLookup.Values;

            if (!blocksets.Any(a => a.Blocks.Any(b => b.Key.Equals(key))))
            {
                BlockSetsLookup.Add(key, blockSet);
            }

            this.lruList.AddLast(cacheItem);
        }