< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.NameValueCache<T>
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/NameValueCache.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 16
Coverable lines: 16
Total lines: 56
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
.ctor(...)100%110%
Lookup(...)100%110%
AddOrUpdate(...)0%440%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/NameValueCache.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Collections;
 5
 6namespace CoreWCF.Dispatcher
 7{
 8    internal class NameValueCache<T>
 9    {
 10        // The NameValueCache implements a structure that uses a dictionary to map objects to
 11        // indices of an array of cache entries.  This allows us to store the cache entries in
 12        // the order in which they were added to the cache, and yet still lookup any cache entry.
 13        // The eviction policy of the cache is to evict the least-recently-added cache entry.
 14        // Using a pointer to the next available cache entry in the array, we can always be sure
 15        // that the given entry is the oldest entry.
 16        private readonly Hashtable _cache;
 17        private readonly string[] _currentKeys;
 18        private int _nextAvailableCacheIndex;
 19        internal const int MaxNumberofEntriesInCache = 16;
 20
 21        public NameValueCache()
 022            : this(MaxNumberofEntriesInCache)
 23        {
 024        }
 25
 026        public NameValueCache(int maxCacheEntries)
 27        {
 028            _cache = new Hashtable();
 029            _currentKeys = new string[maxCacheEntries];
 030        }
 31
 32        public T Lookup(string key)
 33        {
 034            return (T)_cache[key];
 35        }
 36
 37        public void AddOrUpdate(string key, T value)
 38        {
 039            lock (_cache)
 40            {
 041                if (!_cache.ContainsKey(key))
 42                {
 043                    if (!string.IsNullOrEmpty(_currentKeys[_nextAvailableCacheIndex]))
 44                    {
 045                        _cache.Remove(_currentKeys[_nextAvailableCacheIndex]);
 46                    }
 47
 048                    _currentKeys[_nextAvailableCacheIndex] = key;
 049                    _nextAvailableCacheIndex = ++_nextAvailableCacheIndex % _currentKeys.Length;
 50                }
 51
 052                _cache[key] = value;
 053            }
 054        }
 55    }
 56}