| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="CacheStorage.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Threading; |
| | | 11 | | using MyNet.Utilities.Caching.Policies; |
| | | 12 | | |
| | | 13 | | namespace MyNet.Utilities.Caching; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// The cache storage. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <typeparam name="TKey">The key type.</typeparam> |
| | | 19 | | /// <typeparam name="TValue">The value type.</typeparam> |
| | | 20 | | /// <remarks> |
| | | 21 | | /// Initializes a new instance of the <see cref="CacheStorage{TKey,TValue}" /> class. |
| | | 22 | | /// </remarks> |
| | | 23 | | /// <param name="defaultExpirationPolicyInitCode">The default expiration policy initialization code.</param> |
| | | 24 | | /// <param name="storeNullValues">Allow store null values on the cache.</param> |
| | | 25 | | /// <param name="equalityComparer">The equality comparer.</param> |
| | | 26 | | public sealed class CacheStorage<TKey, TValue>(Func<ExpirationPolicy>? defaultExpirationPolicyInitCode = null, bool stor |
| | | 27 | | IEqualityComparer<TKey>? equalityComparer = null) : ICacheStorage<TKey, TValue> |
| | | 28 | | where TKey : notnull |
| | | 29 | | { |
| | | 30 | | #region Fields |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Determines whether the cache storage can store null values. |
| | | 34 | | /// </summary> |
| | 18 | 35 | | private readonly bool _storeNullValues = storeNullValues; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// The dictionary. |
| | | 39 | | /// </summary> |
| | 18 | 40 | | private readonly Dictionary<TKey, CacheStorageValueInfo<TValue>> _dictionary = new(equalityComparer ?? EqualityCompa |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// The synchronization object. |
| | | 44 | | /// </summary> |
| | 18 | 45 | | private readonly Lock _syncObj = new(); |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// The timer that is being executed to invalidate the cache. |
| | | 49 | | /// </summary> |
| | | 50 | | private Timer? _expirationTimer; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Determines whether the cache storage can check for expired items. |
| | | 54 | | /// </summary> |
| | | 55 | | private bool _checkForExpiredItems; |
| | | 56 | | |
| | | 57 | | #endregion |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Occurs when the item is expiring. |
| | | 61 | | /// </summary> |
| | | 62 | | public event EventHandler<ExpiringEventArgs<TKey, TValue>>? Expiring; |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Occurs when the item has expired. |
| | | 66 | | /// </summary> |
| | | 67 | | public event EventHandler<ExpiredEventArgs<TKey, TValue>>? Expired; |
| | | 68 | | |
| | | 69 | | #region ICacheStorage<TKey,TValue> Members |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets or sets a value indicating whether values should be disposed on removal. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <value><c>true</c> if values should be disposed on removal; otherwise, <c>false</c>.</value> |
| | | 75 | | public bool DisposeValuesOnRemoval { get; set; } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets the keys so it is possible to enumerate the cache. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <value>The keys.</value> |
| | 0 | 81 | | public IEnumerable<TKey> Keys => GetKeysSnapshot(); |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets or sets the expiration timer interval. |
| | | 85 | | /// <para /> |
| | | 86 | | /// The default value is <c>TimeSpan.FromSeconds(1)</c>. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <value>The expiration timer interval.</value> |
| | | 89 | | public TimeSpan ExpirationTimerInterval |
| | | 90 | | { |
| | | 91 | | get |
| | 0 | 92 | | { |
| | | 93 | | lock (_syncObj) |
| | | 94 | | { |
| | 0 | 95 | | return _expirationTimerInterval; |
| | | 96 | | } |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | set |
| | | 100 | | { |
| | 9 | 101 | | if (value <= TimeSpan.Zero) |
| | | 102 | | { |
| | 6 | 103 | | throw new ArgumentOutOfRangeException(nameof(value), value, "Expiration timer interval must be greater t |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | lock (_syncObj) |
| | | 107 | | { |
| | 3 | 108 | | _expirationTimerInterval = value; |
| | 3 | 109 | | if (_checkForExpiredItems) |
| | | 110 | | { |
| | 0 | 111 | | UpdateTimerUnsafe(); |
| | | 112 | | } |
| | 3 | 113 | | } |
| | 3 | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | 18 | 117 | | private TimeSpan _expirationTimerInterval = TimeSpan.FromSeconds(1); |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Gets the value associated with the specified key. |
| | | 121 | | /// </summary> |
| | | 122 | | /// <param name="key">The key.</param> |
| | | 123 | | /// <returns>The value associated with the specified key, or default value for the type of the value if the key do n |
| | | 124 | | /// <exception cref="ArgumentNullException">The <paramref name="key" /> is <c>null</c>.</exception> |
| | 6 | 125 | | public TValue? this[TKey key] => Get(key); |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Gets the value associated with the specified <paramref name="key"/>. |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="key">The key of the value to get.</param> |
| | | 131 | | /// <returns>The value associated with the specified key, or default value for the type of the value if the key do n |
| | | 132 | | /// <exception cref="ArgumentNullException">The <paramref name="key" /> is <c>null</c>.</exception> |
| | | 133 | | public TValue? Get(TKey key) |
| | | 134 | | { |
| | 12 | 135 | | ArgumentNullException.ThrowIfNull(key); |
| | | 136 | | |
| | | 137 | | lock (_syncObj) |
| | | 138 | | { |
| | 9 | 139 | | return !TryGetValueInfoUnsafe(key, out var valueInfo) ? default : valueInfo.Value; |
| | | 140 | | } |
| | 9 | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Determines whether the cache contains a value associated with the specified key. |
| | | 145 | | /// </summary> |
| | | 146 | | /// <param name="key">The key.</param> |
| | | 147 | | /// <returns><c>true</c> if the cache contains an element with the specified key; otherwise, <c>false</c>.</returns> |
| | | 148 | | /// <exception cref="ArgumentNullException">The <paramref name="key" /> is <c>null</c>.</exception> |
| | | 149 | | public bool Contains(TKey key) |
| | | 150 | | { |
| | 9 | 151 | | ArgumentNullException.ThrowIfNull(key); |
| | | 152 | | |
| | | 153 | | lock (_syncObj) |
| | | 154 | | { |
| | 6 | 155 | | return TryGetValueInfoUnsafe(key, out _); |
| | | 156 | | } |
| | 6 | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Adds a value to the cache associated with to a key. |
| | | 161 | | /// </summary> |
| | | 162 | | /// <param name="key">The key.</param> |
| | | 163 | | /// <param name="code">The deferred initialization code of the value.</param> |
| | | 164 | | /// <param name="expirationPolicy">The expiration policy.</param> |
| | | 165 | | /// <param name="override">Indicates if the key exists the value will be overridden.</param> |
| | | 166 | | /// <returns>The instance initialized by the <paramref name="code" />.</returns> |
| | | 167 | | /// <exception cref="ArgumentNullException">If <paramref name="key" /> is <c>null</c>.</exception> |
| | | 168 | | /// <exception cref="ArgumentNullException">If <paramref name="code" /> is <c>null</c>.</exception> |
| | | 169 | | public TValue GetFromCacheOrFetch(TKey key, Func<TValue> code, ExpirationPolicy? expirationPolicy, bool @override = |
| | | 170 | | { |
| | 18 | 171 | | ArgumentNullException.ThrowIfNull(key); |
| | 15 | 172 | | ArgumentNullException.ThrowIfNull(code); |
| | | 173 | | |
| | | 174 | | lock (_syncObj) |
| | | 175 | | { |
| | 15 | 176 | | if (!@override && TryGetValueInfoUnsafe(key, out var cacheStorageValueInfo)) |
| | | 177 | | { |
| | 3 | 178 | | return cacheStorageValueInfo.Value; |
| | | 179 | | } |
| | | 180 | | |
| | 12 | 181 | | var value = code(); |
| | 12 | 182 | | if (value is null && !_storeNullValues) |
| | | 183 | | { |
| | 0 | 184 | | return value; |
| | | 185 | | } |
| | | 186 | | |
| | 12 | 187 | | expirationPolicy ??= defaultExpirationPolicyInitCode?.Invoke(); |
| | | 188 | | |
| | 12 | 189 | | _dictionary[key] = new(value, expirationPolicy); |
| | 12 | 190 | | UpdateExpirationStateUnsafe(); |
| | | 191 | | |
| | 12 | 192 | | return value; |
| | | 193 | | } |
| | 15 | 194 | | } |
| | | 195 | | |
| | | 196 | | /// <summary> |
| | | 197 | | /// Adds a value to the cache associated with to a key. |
| | | 198 | | /// </summary> |
| | | 199 | | /// <param name="key">The key.</param> |
| | | 200 | | /// <param name="code">The deferred initialization code of the value.</param> |
| | | 201 | | /// <param name="override">Indicates if the key exists the value will be overridden.</param> |
| | | 202 | | /// <param name="expiration">The timespan in which the cache item should expire when added.</param> |
| | | 203 | | /// <returns>The instance initialized by the <paramref name="code" />.</returns> |
| | | 204 | | /// <exception cref="ArgumentNullException">If <paramref name="key" /> is <c>null</c>.</exception> |
| | | 205 | | /// <exception cref="ArgumentNullException">If <paramref name="code" /> is <c>null</c>.</exception> |
| | 9 | 206 | | public TValue GetFromCacheOrFetch(TKey key, Func<TValue> code, bool @override = false, TimeSpan expiration = default |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Adds a value to the cache associated with to a key. |
| | | 210 | | /// </summary> |
| | | 211 | | /// <param name="key">The key.</param> |
| | | 212 | | /// <param name="value">The value.</param> |
| | | 213 | | /// <param name="override">Indicates if the key exists the value will be overridden.</param> |
| | | 214 | | /// <param name="expiration">The timespan in which the cache item should expire when added.</param> |
| | | 215 | | /// <exception cref="ArgumentNullException">The <paramref name="key" /> is <c>null</c>.</exception> |
| | 6 | 216 | | public void Add(TKey key, TValue value, bool @override = false, TimeSpan expiration = default) => Add(key, value, Ex |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// Adds a value to the cache associated with to a key. |
| | | 220 | | /// </summary> |
| | | 221 | | /// <param name="key">The key.</param> |
| | | 222 | | /// <param name="value">The value.</param> |
| | | 223 | | /// <param name="expirationPolicy">The expiration policy.</param> |
| | | 224 | | /// <param name="override">Indicates if the key exists the value will be overridden.</param> |
| | | 225 | | /// <exception cref="ArgumentNullException">The <paramref name="key" /> is <c>null</c>.</exception> |
| | 6 | 226 | | public void Add(TKey key, TValue value, ExpirationPolicy? expirationPolicy, bool @override = false) => GetFromCacheO |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// Removes an item from the cache. |
| | | 230 | | /// </summary> |
| | | 231 | | /// <param name="key">The key.</param> |
| | | 232 | | /// <param name="action">The action that need to be executed in synchronization with the item cache removal.</param> |
| | | 233 | | /// <exception cref="ArgumentNullException">The <paramref name="key" /> is <c>null</c>.</exception> |
| | | 234 | | public void Remove(TKey key, Action? action = null) |
| | | 235 | | { |
| | 3 | 236 | | ArgumentNullException.ThrowIfNull(key); |
| | | 237 | | |
| | | 238 | | lock (_syncObj) |
| | | 239 | | { |
| | 0 | 240 | | _ = RemoveItemUnsafe(key, false, action); |
| | 0 | 241 | | } |
| | 0 | 242 | | } |
| | | 243 | | |
| | | 244 | | /// <summary> |
| | | 245 | | /// Clears all the items currently in the cache. |
| | | 246 | | /// </summary> |
| | | 247 | | public void Clear() |
| | 0 | 248 | | { |
| | | 249 | | lock (_syncObj) |
| | | 250 | | { |
| | 0 | 251 | | var keysToRemove = _dictionary.Keys.ToList(); |
| | 0 | 252 | | foreach (var keyToRemove in keysToRemove) |
| | | 253 | | { |
| | 0 | 254 | | _ = RemoveItemUnsafe(keyToRemove, false); |
| | | 255 | | } |
| | | 256 | | |
| | 0 | 257 | | UpdateExpirationStateUnsafe(); |
| | 0 | 258 | | } |
| | 0 | 259 | | } |
| | | 260 | | |
| | | 261 | | private TKey[] GetKeysSnapshot() |
| | 0 | 262 | | { |
| | | 263 | | lock (_syncObj) |
| | | 264 | | { |
| | 0 | 265 | | return [.. _dictionary.Keys]; |
| | | 266 | | } |
| | 0 | 267 | | } |
| | | 268 | | |
| | | 269 | | /// <summary> |
| | | 270 | | /// Removes the expired items from the cache. |
| | | 271 | | /// </summary> |
| | | 272 | | private void RemoveExpiredItems() |
| | 3 | 273 | | { |
| | | 274 | | lock (_syncObj) |
| | | 275 | | { |
| | 3 | 276 | | if (!_checkForExpiredItems) |
| | | 277 | | { |
| | 0 | 278 | | return; |
| | | 279 | | } |
| | | 280 | | |
| | 3 | 281 | | var keysToRemove = new List<TKey>(); |
| | 12 | 282 | | foreach (var (key, valueInfo) in _dictionary) |
| | | 283 | | { |
| | 3 | 284 | | if (valueInfo.IsExpired) |
| | | 285 | | { |
| | 3 | 286 | | keysToRemove.Add(key); |
| | | 287 | | } |
| | | 288 | | } |
| | | 289 | | |
| | 12 | 290 | | foreach (var keyToRemove in keysToRemove) |
| | | 291 | | { |
| | 3 | 292 | | _ = RemoveItemUnsafe(keyToRemove, true); |
| | | 293 | | } |
| | | 294 | | |
| | 3 | 295 | | UpdateExpirationStateUnsafe(); |
| | 3 | 296 | | } |
| | 3 | 297 | | } |
| | | 298 | | |
| | | 299 | | /// <summary> |
| | | 300 | | /// Called when the timer to clean up the cache elapsed. |
| | | 301 | | /// </summary> |
| | | 302 | | /// <param name="state">The timer state.</param> |
| | 3 | 303 | | private void OnTimerElapsed(object? state) => RemoveExpiredItems(); |
| | | 304 | | |
| | | 305 | | private bool TryGetValueInfoUnsafe(TKey key, out CacheStorageValueInfo<TValue> valueInfo) |
| | | 306 | | { |
| | 30 | 307 | | if (!_dictionary.TryGetValue(key, out var cacheStorageValueInfo)) |
| | | 308 | | { |
| | 18 | 309 | | valueInfo = null!; |
| | 18 | 310 | | return false; |
| | | 311 | | } |
| | | 312 | | |
| | 12 | 313 | | valueInfo = cacheStorageValueInfo; |
| | | 314 | | |
| | 12 | 315 | | if (!valueInfo.IsExpired) |
| | | 316 | | { |
| | 9 | 317 | | return true; |
| | | 318 | | } |
| | | 319 | | |
| | 3 | 320 | | _ = RemoveItemUnsafe(key, true); |
| | | 321 | | |
| | 3 | 322 | | valueInfo = null!; |
| | 3 | 323 | | return false; |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | /// <summary> |
| | | 327 | | /// Remove item from cache by key. |
| | | 328 | | /// </summary> |
| | | 329 | | /// <param name="key">The key.</param> |
| | | 330 | | /// <param name="raiseEvents">Indicates whether events should be raised.</param> |
| | | 331 | | /// <param name="action">The action that need to be executed in synchronization with the item cache removal.</param> |
| | | 332 | | /// <returns>The value indicating whether the item was removed.</returns> |
| | | 333 | | private bool RemoveItemUnsafe(TKey key, bool raiseEvents, Action? action = null) |
| | | 334 | | { |
| | | 335 | | // Try to get item, if there is no item by that key then return true to indicate that item was removed. |
| | 6 | 336 | | if (!_dictionary.TryGetValue(key, out var item)) |
| | | 337 | | { |
| | 0 | 338 | | return true; |
| | | 339 | | } |
| | | 340 | | |
| | 6 | 341 | | action?.Invoke(); |
| | | 342 | | |
| | 6 | 343 | | var cancel = false; |
| | 6 | 344 | | var expirationPolicy = item.ExpirationPolicy; |
| | 6 | 345 | | if (raiseEvents) |
| | | 346 | | { |
| | 6 | 347 | | var expiringEventArgs = new ExpiringEventArgs<TKey, TValue>(key, item.Value, expirationPolicy); |
| | 6 | 348 | | Expiring?.Invoke(this, expiringEventArgs); |
| | | 349 | | |
| | 6 | 350 | | cancel = expiringEventArgs.Cancel; |
| | 6 | 351 | | expirationPolicy = expiringEventArgs.ExpirationPolicy; |
| | | 352 | | } |
| | | 353 | | |
| | 6 | 354 | | if (cancel) |
| | | 355 | | { |
| | 0 | 356 | | expirationPolicy ??= defaultExpirationPolicyInitCode?.Invoke(); |
| | | 357 | | |
| | 0 | 358 | | _dictionary[key] = new(item.Value, expirationPolicy); |
| | 0 | 359 | | UpdateExpirationStateUnsafe(); |
| | | 360 | | |
| | 0 | 361 | | return false; |
| | | 362 | | } |
| | | 363 | | |
| | 6 | 364 | | _ = _dictionary.Remove(key); |
| | | 365 | | |
| | 6 | 366 | | var dispose = DisposeValuesOnRemoval; |
| | 6 | 367 | | if (raiseEvents) |
| | | 368 | | { |
| | 6 | 369 | | var expiredEventArgs = new ExpiredEventArgs<TKey, TValue>(key, item.Value, dispose); |
| | 6 | 370 | | Expired?.Invoke(this, expiredEventArgs); |
| | | 371 | | |
| | 6 | 372 | | dispose = expiredEventArgs.Dispose; |
| | | 373 | | } |
| | | 374 | | |
| | 6 | 375 | | if (dispose) |
| | | 376 | | { |
| | 0 | 377 | | item.DisposeValue(); |
| | | 378 | | } |
| | | 379 | | |
| | 6 | 380 | | UpdateExpirationStateUnsafe(); |
| | | 381 | | |
| | 6 | 382 | | return true; |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | private void UpdateExpirationStateUnsafe() |
| | | 386 | | { |
| | 21 | 387 | | var containsItemsThatCanExpire = _dictionary.Values.Any(x => x.CanExpire); |
| | 21 | 388 | | if (_checkForExpiredItems == containsItemsThatCanExpire) |
| | | 389 | | { |
| | 9 | 390 | | return; |
| | | 391 | | } |
| | | 392 | | |
| | 12 | 393 | | _checkForExpiredItems = containsItemsThatCanExpire; |
| | 12 | 394 | | UpdateTimerUnsafe(); |
| | 12 | 395 | | } |
| | | 396 | | |
| | | 397 | | private void UpdateTimerUnsafe() |
| | | 398 | | { |
| | 12 | 399 | | if (!_checkForExpiredItems) |
| | | 400 | | { |
| | 6 | 401 | | _expirationTimer?.Dispose(); |
| | 6 | 402 | | _expirationTimer = null; |
| | 6 | 403 | | return; |
| | | 404 | | } |
| | | 405 | | |
| | 6 | 406 | | var timeSpan = _expirationTimerInterval; |
| | 6 | 407 | | _expirationTimer ??= new(OnTimerElapsed, null, Timeout.Infinite, Timeout.Infinite); |
| | 6 | 408 | | _ = _expirationTimer.Change(timeSpan, timeSpan); |
| | 6 | 409 | | } |
| | | 410 | | #endregion |
| | | 411 | | } |
| | | 412 | | |