| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Collections; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Threading; |
| | 9 | |
|
| | 10 | | namespace Microsoft.Azure.KeyVault |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// A simple Least Recently Used Cache |
| | 14 | | /// </summary> |
| | 15 | | /// <typeparam name="K">The type of the key</typeparam> |
| | 16 | | /// <typeparam name="V">The type of the value</typeparam> |
| | 17 | | internal class LRUCache<K, V> : IDisposable, IEnumerable<V> where K : class where V : class |
| | 18 | | { |
| | 19 | | private int _capacity; |
| 12 | 20 | | private Dictionary<K, V> _cache = new Dictionary<K, V>(); |
| 12 | 21 | | private LinkedList<K> _list = new LinkedList<K>(); |
| 12 | 22 | | private ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(); |
| | 23 | | private bool _isDisposed; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Constructor. |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="capacity">The maximum capacity of the cache</param> |
| 12 | 29 | | public LRUCache( int capacity ) |
| | 30 | | { |
| 12 | 31 | | if ( capacity <= 0 ) |
| 0 | 32 | | throw new ArgumentException( "Capacity must be a positive non-zero value" ); |
| | 33 | |
|
| 12 | 34 | | _capacity = capacity; |
| 12 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Adds a key and value to the cache. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="key">The key</param> |
| | 41 | | /// <param name="value">The value</param> |
| | 42 | | public void Add( K key, V value ) |
| | 43 | | { |
| 32 | 44 | | if ( _isDisposed ) |
| 0 | 45 | | throw new ObjectDisposedException( "LRUCache" ); |
| | 46 | |
|
| 32 | 47 | | if ( key == null ) |
| 0 | 48 | | throw new ArgumentNullException( "key" ); |
| | 49 | |
|
| 32 | 50 | | if ( value == null ) |
| 0 | 51 | | throw new ArgumentNullException( "value" ); |
| | 52 | |
|
| | 53 | | try |
| | 54 | | { |
| 32 | 55 | | _lock.EnterWriteLock(); |
| | 56 | |
|
| 32 | 57 | | if ( !_cache.ContainsKey( key ) ) |
| | 58 | | { |
| | 59 | | // Cache before List as the cache may throw an exception |
| 18 | 60 | | _cache.Add(key, value); |
| 18 | 61 | | _list.AddLast( key ); |
| | 62 | |
|
| 18 | 63 | | if ( _list.Count > _capacity ) |
| | 64 | | { |
| 4 | 65 | | LinkedListNode<K> lruKey = _list.First; |
| | 66 | |
|
| 4 | 67 | | _cache.Remove( lruKey.Value ); |
| 4 | 68 | | _list.RemoveFirst(); |
| | 69 | | } |
| | 70 | | } |
| 32 | 71 | | } |
| | 72 | | finally |
| | 73 | | { |
| 32 | 74 | | _lock.ExitWriteLock(); |
| 32 | 75 | | } |
| 32 | 76 | | } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Gets a value from the cache. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="key">The key</param> |
| | 82 | | /// <returns>The value for the key or null</returns> |
| | 83 | | public V Get( K key ) |
| | 84 | | { |
| 108 | 85 | | if ( _isDisposed ) |
| 0 | 86 | | throw new ObjectDisposedException( "LRUCache" ); |
| | 87 | |
|
| 108 | 88 | | if ( key == null ) |
| 0 | 89 | | throw new ArgumentNullException( "key" ); |
| | 90 | |
|
| 108 | 91 | | V value = null; |
| | 92 | |
|
| | 93 | | try |
| | 94 | | { |
| 108 | 95 | | _lock.EnterUpgradeableReadLock(); |
| | 96 | |
|
| 108 | 97 | | if ( _cache.TryGetValue( key, out value ) ) |
| | 98 | | { |
| | 99 | | try |
| | 100 | | { |
| 72 | 101 | | _lock.EnterWriteLock(); |
| | 102 | |
|
| | 103 | | // Move the key to the tail of the LRU list |
| 72 | 104 | | _list.Remove( key ); |
| 72 | 105 | | _list.AddLast( key ); |
| 72 | 106 | | } |
| | 107 | | finally |
| | 108 | | { |
| 72 | 109 | | _lock.ExitWriteLock(); |
| 72 | 110 | | } |
| | 111 | | } |
| 36 | 112 | | } |
| | 113 | | finally |
| | 114 | | { |
| 108 | 115 | | _lock.ExitUpgradeableReadLock(); |
| 108 | 116 | | } |
| | 117 | |
|
| 108 | 118 | | return value; |
| | 119 | | } |
| | 120 | |
|
| | 121 | | /// <summary> |
| | 122 | | /// Removes a key and its value from the cache |
| | 123 | | /// </summary> |
| | 124 | | /// <param name="key">The key to remove</param> |
| | 125 | | public void Remove( K key ) |
| | 126 | | { |
| 0 | 127 | | if ( _isDisposed ) |
| 0 | 128 | | throw new ObjectDisposedException( "LRUCache" ); |
| | 129 | |
|
| 0 | 130 | | if ( key == null ) |
| 0 | 131 | | throw new ArgumentNullException( "key" ); |
| | 132 | |
|
| | 133 | | try |
| | 134 | | { |
| 0 | 135 | | _lock.EnterUpgradeableReadLock(); |
| | 136 | |
|
| 0 | 137 | | if ( _cache.ContainsKey( key ) ) |
| | 138 | | { |
| 0 | 139 | | _cache.Remove( key ); |
| 0 | 140 | | _list.Remove( key ); |
| | 141 | | } |
| 0 | 142 | | } |
| | 143 | | finally |
| | 144 | | { |
| 0 | 145 | | _lock.ExitUpgradeableReadLock(); |
| 0 | 146 | | } |
| 0 | 147 | | } |
| | 148 | |
|
| | 149 | | /// <summary> |
| | 150 | | /// Resets the content of the cache. |
| | 151 | | /// </summary> |
| | 152 | | public void Reset() |
| | 153 | | { |
| 0 | 154 | | if ( _isDisposed ) |
| 0 | 155 | | throw new ObjectDisposedException( "LRUCache" ); |
| | 156 | |
|
| | 157 | | try |
| | 158 | | { |
| 0 | 159 | | _lock.EnterWriteLock(); |
| | 160 | |
|
| 0 | 161 | | _cache.Clear(); |
| 0 | 162 | | _list.Clear(); |
| 0 | 163 | | } |
| | 164 | | finally |
| | 165 | | { |
| 0 | 166 | | _lock.ExitWriteLock(); |
| 0 | 167 | | } |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | IEnumerator IEnumerable.GetEnumerator() |
| | 171 | | { |
| 12 | 172 | | return GetEnumerator(); |
| | 173 | | } |
| | 174 | |
|
| | 175 | | /// <summary> |
| | 176 | | /// Get enumerator on the cached values |
| | 177 | | /// </summary> |
| | 178 | | /// <returns></returns> |
| | 179 | | public IEnumerator<V> GetEnumerator() |
| | 180 | | { |
| 12 | 181 | | return _cache.Values.GetEnumerator(); |
| | 182 | | } |
| | 183 | |
|
| | 184 | | public void Dispose() |
| | 185 | | { |
| 12 | 186 | | Dispose( true ); |
| 12 | 187 | | GC.SuppressFinalize( this ); |
| 12 | 188 | | } |
| | 189 | |
|
| | 190 | | protected virtual void Dispose( bool disposing ) |
| | 191 | | { |
| 12 | 192 | | if ( disposing ) |
| | 193 | | { |
| 12 | 194 | | if ( !_isDisposed ) |
| | 195 | | { |
| 12 | 196 | | _isDisposed = true; |
| 12 | 197 | | _lock.Dispose(); |
| 12 | 198 | | _lock = null; |
| | 199 | | } |
| | 200 | | } |
| 12 | 201 | | } |
| | 202 | | } |
| | 203 | | } |