예제 #1
0
        /// <summary>
        /// Attempt to insert item into cache.
        /// </summary>
        /// <param name="key">Key of the item to be inserted in the cache.</param>
        /// <param name="value">Value of the item to be inserted in the cache.</param>
        /// <returns><c>true</c> when the key does not already exist in the cache, <c>false</c> otherwise.</returns>
        public bool TryAddValue(TKey key, TValue value)
        {
            lock (_dictionary)
            {
                MruCacheItem item;
                if (_dictionary.TryGetValue(key, out item))
                {
                    var version = _currentVersion;
                    if (item.Version != version || !EqualityComparer <TValue> .Default.Equals(value, item.Value))
                    {
                        _dictionary[key] = new MruCacheItem(value, version, false);
                    }
                    return(false);   // Already exists
                }
                else
                {
                    if (_dictionary.Count >= _maxCapacity)
                    {
                        ++_currentVersion;
                        PruneCache();
                    }

                    _dictionary.Add(key, new MruCacheItem(value, _currentVersion, true));
                    return(true);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Lookup existing item in cache.
        /// </summary>
        /// <param name="key">Key of the item to be searched in the cache.</param>
        /// <param name="value">Output value of the item found in the cache.</param>
        /// <returns><c>True</c> when the key is found in the cache, <c>false</c> otherwise.</returns>
        public bool TryGetValue(TKey key, out TValue value)
        {
            MruCacheItem item;

            try
            {
                if (!_dictionary.TryGetValue(key, out item))
                {
                    value = default(TValue);
                    return(false);
                }
            }
            catch
            {
                item = default(MruCacheItem);    // Too many people in the same room
            }

            if (item.Version != _currentVersion || item.Virgin)
            {
                // Update item version to mark as recently used
                lock (_dictionary)
                {
                    var version = _currentVersion;
                    if (_dictionary.TryGetValue(key, out item))
                    {
                        if (item.Version != version || item.Virgin)
                        {
                            if (item.Virgin)
                            {
                                version = ++_currentVersion;
                            }
                            _dictionary[key] = new MruCacheItem(item.Value, version, false);
                        }
                    }
                    else
                    {
                        value = default(TValue);
                        return(false);
                    }
                }
            }

            value = item.Value;
            return(true);
        }