• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

microsoft / botbuilder-dotnet / 361082

21 Jul 2023 05:59PM UTC coverage: 78.959% (-0.006%) from 78.965%
361082

Pull #6675

CI-PR build

web-flow
Merge 2bfac55b6 into 6a17a29e7
Pull Request #6675: Updated stray Microsoft.Rest.ClientRuntime dependency

25938 of 32850 relevant lines covered (78.96%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.18
/libraries/AdaptiveExpressions/LRUCache.cs
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
// Licensed under the MIT License.
3

4
using System.Collections.Generic;
5

6
namespace AdaptiveExpressions
7
{
8
    /// <summary>
9
    /// A least-recently-used cache stored like a dictionary.
10
    /// </summary>
11
    /// <typeparam name="TKey">The type of the key to the cached item.</typeparam>
12
    /// <typeparam name="TValue">The type of the cached item.</typeparam>
13
    public sealed class LRUCache<TKey, TValue>
14
    {
15
        /// <summary>
16
        /// Default maximum number of elements to cache.
17
        /// </summary>
18
        private const int DefaultCapacity = 255;
19

20
        private readonly object _lockObj = new object();
1✔
21
        private readonly int _capacity;
22
        private readonly Dictionary<TKey, Entry> _cacheMap;
23
        private readonly LinkedList<TKey> _cacheList;
24

25
        /// <summary>
26
        /// Initializes a new instance of the <see cref="LRUCache{TKey, TValue}"/> class.
27
        /// </summary>
28
        public LRUCache()
29
            : this(DefaultCapacity)
1✔
30
        {
31
        }
1✔
32

33
        /// <summary>
34
        /// Initializes a new instance of the <see cref="LRUCache{TKey, TValue}"/> class.
35
        /// </summary>
36
        /// <param name="capacity">Maximum number of elements to cache.</param>
37
        public LRUCache(int capacity)
1✔
38
        {
39
            _capacity = capacity > 0 ? capacity : DefaultCapacity;
×
40
            _cacheMap = new Dictionary<TKey, Entry>();
1✔
41
            _cacheList = new LinkedList<TKey>();
1✔
42
        }
1✔
43

44
        /// <summary>
45
        /// Gets the value associated with the specified key.
46
        /// </summary>
47
        /// <param name="key">The key of the value to get.</param>
48
        /// <param name="value">When this method returns, contains the value associated with
49
        /// the specified key, if the key is found; otherwise, the default value for the 
50
        /// type of the <paramref name="value" /> parameter.</param>
51
        /// <returns>true if contains an element with the specified key; otherwise, false.</returns>
52
        public bool TryGet(TKey key, out TValue value)
53
        {
54
            lock (_lockObj)
1✔
55
            {
56
                if (_cacheMap.TryGetValue(key, out var entry))
1✔
57
                {
58
                    Touch(entry.Node);
1✔
59
                    value = entry.Value;
1✔
60
                    return true;
1✔
61
                }
62
            }
1✔
63

64
            value = default(TValue);
1✔
65
            return false;
1✔
66
        }
1✔
67

68
        /// <summary>
69
        /// Adds the specified key and value to the cache.
70
        /// </summary>
71
        /// <param name="key">The key of the element to add.</param>
72
        /// <param name="value">The value of the element to add.</param>
73
        public void Set(TKey key, TValue value)
74
        {
75
            lock (_lockObj)
1✔
76
            {
77
                if (!_cacheMap.TryGetValue(key, out var entry))
1✔
78
                {
79
                    LinkedListNode<TKey> node;
80
                    if (_cacheMap.Count >= _capacity)
1✔
81
                    {
82
                        node = _cacheList.Last;
1✔
83
                        _cacheMap.Remove(node.Value);
1✔
84
                        _cacheList.RemoveLast();
1✔
85
                        node.Value = key;
1✔
86
                    }
87
                    else
88
                    {
89
                        node = new LinkedListNode<TKey>(key);
1✔
90
                    }
91

92
                    _cacheList.AddFirst(node);
1✔
93
                    _cacheMap.Add(key, new Entry(node, value));
1✔
94
                }
95
                else
96
                {
97
                    entry.Value = value;
×
98
                    _cacheMap[key] = entry;
×
99
                    Touch(entry.Node);
×
100
                }
101
            }
×
102
        }
1✔
103

104
        private void Touch(LinkedListNode<TKey> node)
105
        {
106
            if (node != _cacheList.First)
1✔
107
            {
108
                _cacheList.Remove(node);
1✔
109
                _cacheList.AddFirst(node);
1✔
110
            }
111
        }
1✔
112

113
        private struct Entry
114
        {
115
            public LinkedListNode<TKey> Node;
116
            public TValue Value;
117

118
            public Entry(LinkedListNode<TKey> node, TValue value)
119
            {
120
                this.Node = node;
1✔
121
                this.Value = value;
1✔
122
            }
1✔
123
        }
124
    }
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc