Esempio n. 1
0
 public IEnumerator <KeyValuePair <TKey, TValue> > GetEnumerator()
 {
     return(EntriesAsCollection
            .Where(e => !e.Value.Expired)
            .Select(e => new KeyValuePair <TKey, TValue>(e.Key, e.Value.Data))
            .GetEnumerator());
 }
Esempio n. 2
0
        public bool Remove(KeyValuePair <TKey, TValue> item)
        {
            EntryData entryData;

            return(_Entries.TryGetValue(item.Key, out entryData) &&
                   (Object.Equals(entryData.Data, item.Value)) &&
                   EntriesAsCollection.Remove(new KeyValuePair <TKey, EntryData>(item.Key, entryData)));
        }
Esempio n. 3
0
        private void Clean()
        {
            KeyValuePair <TKey, EntryData> queued;
            bool expiredEntries = _LastWrite.TryPeek(out queued) && queued.Value.Expired;

            while (expiredEntries || (_Entries.Count > MaxEntries))
            {
                // TODO: Ideally we'd have a queue where we could peek first, check if it's something we want,
                // then dequeue iff what's @ the head == what we peeked. But this is good enough - at worst
                // we'll have a slightly over-aggressive clean
                if (!_LastWrite.TryDequeue(out queued))
                {
                    break;
                }

                expiredEntries = queued.Value.Expired;
                EntriesAsCollection.Remove(queued);
            }
        }
Esempio n. 4
0
        /**
         * Gets the existing value if the key was present, or adds a new value if the key was not present.
         *
         * If the value was added, returns true and sets currentValue = newValue.
         *
         * If the value already existed, returns false and returns the existing value in currentValue.
         **/
        public bool GetOrAdd(TKey key, TValue newValue, out TValue currentValue)
        {
            var newEntry      = GetEntry(newValue);
            var existingEntry = _Entries.GetOrAdd(key, newEntry);

            if (existingEntry != newEntry)
            {
                while ((!existingEntry.GetData(out currentValue)) && (existingEntry != newEntry))
                {
                    EntriesAsCollection.Remove(new KeyValuePair <TKey, EntryData>(key, existingEntry));
                    existingEntry = _Entries.GetOrAdd(key, newEntry);
                }

                if (existingEntry != newEntry)
                {
                    return(false);
                }
            }

            TrackWrite(newEntry, key);

            currentValue = newValue;
            return(true);
        }