| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="CacheStorageValueInfo.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.Utilities.Caching.Policies; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Utilities.Caching; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Value info for the cache storage. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <typeparam name="TValue"> |
| | | 16 | | /// The value type. |
| | | 17 | | /// </typeparam> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// Initializes a new instance of the <see cref="CacheStorageValueInfo{TValue}" /> class. |
| | | 20 | | /// </remarks> |
| | | 21 | | /// <param name="value">The value.</param> |
| | | 22 | | /// <param name="expirationPolicy">The expiration policy.</param> |
| | | 23 | | internal sealed class CacheStorageValueInfo<TValue>(TValue value, ExpirationPolicy? expirationPolicy = null) |
| | | 24 | | { |
| | | 25 | | #region Constructors |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="CacheStorageValueInfo{TValue}" /> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="value">The value.</param> |
| | | 31 | | /// <param name="expiration">The expiration.</param> |
| | | 32 | | public CacheStorageValueInfo(TValue value, TimeSpan expiration) |
| | 0 | 33 | | : this(value, ExpirationPolicy.Duration(expiration)) |
| | | 34 | | { |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | #endregion |
| | | 38 | | |
| | | 39 | | #region Properties |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets the value. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <value>The value.</value> |
| | | 45 | | public TValue Value |
| | | 46 | | { |
| | | 47 | | get |
| | | 48 | | { |
| | 21 | 49 | | if (CanExpire && (ExpirationPolicy?.CanReset ?? false)) |
| | | 50 | | { |
| | 0 | 51 | | ExpirationPolicy.Reset(); |
| | | 52 | | } |
| | | 53 | | |
| | 21 | 54 | | return value; |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Gets a value indicating whether this value can expire. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <value><c>true</c> if this value can expire; otherwise, <c>false</c>.</value> |
| | 48 | 62 | | public bool CanExpire => ExpirationPolicy is not null; |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets a value indicating whether this value is expired. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <value><c>true</c> if this value is expired; otherwise, <c>false</c>.</value> |
| | 15 | 68 | | public bool IsExpired => CanExpire && (ExpirationPolicy?.IsExpired ?? false); |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the expiration policy. |
| | | 72 | | /// </summary> |
| | 12 | 73 | | internal ExpirationPolicy? ExpirationPolicy { get; } = expirationPolicy; |
| | | 74 | | |
| | | 75 | | #endregion |
| | | 76 | | |
| | | 77 | | #region Methods |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Dispose value. |
| | | 81 | | /// </summary> |
| | | 82 | | public void DisposeValue() |
| | | 83 | | { |
| | 0 | 84 | | var disposable = value as IDisposable; |
| | 0 | 85 | | disposable?.Dispose(); |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | #endregion |
| | | 89 | | } |
| | | 90 | | |