Exemplo n.º 1
0
                protected override void ClearCore(bool disposing)
                {
                    _lock.EnterWriteLock();
                    try
                    {
                        foreach (var entry in _entries)
                        {
                            if (entry.Key.TryGetTarget(out T key))
                            {
                                _cache.Remove(key);
                            }
                        }

                        _entries.Clear();
#if DEBUG
                        _invocationCount = 0;
                        _accessCount     = 0;
                        _evictionCount   = 0;
                        _trimCount       = 0;
                        _trimElapsed     = TimeSpan.Zero;
                        _lastEvicted     = null;
#endif
                    }
                    finally
                    {
                        _lock.ExitWriteLock();
                    }
                }
Exemplo n.º 2
0
                private void Trim()
                {
                    if (_cache.Count >= _maxCapacity)
                    {
#if DEBUG
                        var trimStart = _stopwatch.ElapsedTicks;
#endif
#pragma warning disable IDE0079 // Remove unnecessary suppression.
#pragma warning disable IDE0063 // Use simple 'using' statement. (Only in RELEASE build.)
                        using (var evictionOrder = _ranker.GetEnumerator())
                        {
                            while (_cache.Count >= _maxCapacity && evictionOrder.MoveNext())
                            {
                                var entry = evictionOrder.Current;
#if DEBUG
                                _lastEvicted = entry;
#endif
                                _cache.Remove(entry.Key);
#if DEBUG
                                _evictionCount++;
#endif
                            }
                        }
#pragma warning restore IDE0063
#pragma warning restore IDE0079
#if DEBUG
                        var trimElapsed = new TimeSpan(_stopwatch.ElapsedTicks - trimStart);
                        _trimCount++;
                        _trimElapsed += trimElapsed;
#endif
                    }
                }
Exemplo n.º 3
0
                protected override void ClearCore(bool disposing)
                {
                    _cache.Clear();
#if DEBUG
                    _invocationCount = 0;
                    _accessCount     = 0;
                    _evictionCount   = 0;
                    _trimCount       = 0;
                    _trimElapsed     = TimeSpan.Zero;
                    _lastEvicted     = null;
#endif
                }
Exemplo n.º 4
0
                private void Trim()
                {
                    //
                    // NB: We can have temporary oversubscription during concurrent accesses because we avoid to enter the write lock
                    //     until absolutely necessary, so _entries.Count can be a dirty read.
                    //
                    if (_entries.Count >= _maxCapacity)
                    {
#if DEBUG
                        var trimStart = _stopwatch.ElapsedTicks;
#endif
                        _lock.EnterWriteLock();
                        try
                        {
                            using (var evictionOrder = _ranker.GetEnumerator())
                            {
                                while (_entries.Count >= _maxCapacity && evictionOrder.MoveNext())
                                {
                                    var entry = evictionOrder.Current;
#if DEBUG
                                    _lastEvicted = entry;
#endif
                                    if (entry.Key.TryGetTarget(out T key))
                                    {
                                        _cache.Remove(key);
                                    }

                                    _entries.Remove(entry);
#if DEBUG
                                    _evictionCount++;
#endif
                                }
                            }

                            _entries.RemoveWhere(e => !e.Key.TryGetTarget(out _));
                        }
                        finally
                        {
                            _lock.ExitWriteLock();
                        }
#if DEBUG
                        var trimElapsed = new TimeSpan(_stopwatch.ElapsedTicks - trimStart);
                        _trimCount++;
                        _trimElapsed += trimElapsed;
#endif
                    }
                }