| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ExpirationPolicy.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | |
| | | 9 | | namespace MyNet.Utilities.Caching.Policies; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// The expiration policy. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// Initializes a new instance of the <see cref="ExpirationPolicy" /> class. |
| | | 16 | | /// </remarks> |
| | | 17 | | /// <param name="canReset">The can reset.</param> |
| | | 18 | | public abstract class ExpirationPolicy(bool canReset = false) |
| | | 19 | | { |
| | | 20 | | #region Properties |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets a value indicating whether the value with this policy attached is expired. |
| | | 24 | | /// </summary> |
| | | 25 | | public abstract bool IsExpired { get; } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets a value indicating whether the value with this policy can be reset. |
| | | 29 | | /// </summary> |
| | | 30 | | public virtual bool CanReset { get; } = canReset; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets a value indicating whether is resting. |
| | | 34 | | /// </summary> |
| | | 35 | | protected bool IsResting { get; private set; } |
| | | 36 | | #endregion |
| | | 37 | | |
| | | 38 | | #region Methods |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Creates a <see cref="AbsoluteExpirationPolicy" /> instance. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="absoluteExpirationDateTime">The absolute expiration <see cref="DateTime" />.</param> |
| | | 44 | | /// <param name="force">Indicates whether the policy will be created even if the policy will be created expired.</pa |
| | | 45 | | /// <returns>The <see cref="AbsoluteExpirationPolicy" /> or <c>null</c> if <paramref name="absoluteExpirationDateTim |
| | | 46 | | /// <remarks>The cache item will expire on the absolute expiration date time.</remarks> |
| | | 47 | | public static ExpirationPolicy? Absolute(DateTime absoluteExpirationDateTime, bool force = false) |
| | 9 | 48 | | => force || DateTime.UtcNow < absoluteExpirationDateTime.ToUniversalTime() ? new AbsoluteExpirationPolicy(absolu |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Creates a <see cref="DurationExpirationPolicy" /> instance. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="durationTimeSpan">The duration <see cref="TimeSpan" />.</param> |
| | | 54 | | /// <param name="force">Indicates whether the policy will be created even if the policy will be created expired.</pa |
| | | 55 | | /// <returns>The <see cref="DurationExpirationPolicy" /> or <c>null</c> if <paramref name="durationTimeSpan" /> is l |
| | | 56 | | /// <remarks>The cache item will expire using the duration to calculate the absolute expiration from now.</remarks> |
| | | 57 | | public static ExpirationPolicy? Duration(TimeSpan durationTimeSpan, bool force = false) |
| | 21 | 58 | | => force || durationTimeSpan > TimeSpan.Zero ? new DurationExpirationPolicy(durationTimeSpan) : null; |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Creates a <see cref="SlidingExpirationPolicy" /> instance. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="durationTimeSpan">The duration <see cref="TimeSpan" />.</param> |
| | | 64 | | /// <param name="force">Indicates whether the policy will be created even if the policy will be created expired.</pa |
| | | 65 | | /// <returns>The <see cref="SlidingExpirationPolicy" /> or <c>null</c> if <paramref name="durationTimeSpan" /> is le |
| | | 66 | | /// <remarks>The cache item will expire using the duration property as the sliding expiration.</remarks> |
| | | 67 | | public static ExpirationPolicy? Sliding(TimeSpan durationTimeSpan, bool force = false) |
| | 3 | 68 | | => force || durationTimeSpan > TimeSpan.Zero ? new SlidingExpirationPolicy(durationTimeSpan) : null; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Creates a <see cref="CustomExpirationPolicy" /> instance. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <param name="isExpiredFunc">The function to check if the policy is expired.</param> |
| | | 74 | | /// <param name="resetAction">The action that will be executed if the item is read before expiration.</param> |
| | | 75 | | /// <param name="force">Indicates whether the policy will be created even if the policy will be created expired.</pa |
| | | 76 | | /// <returns>The <see cref="CustomExpirationPolicy" />.</returns> |
| | | 77 | | /// <exception cref="ArgumentNullException">The <paramref name="isExpiredFunc" /> is <c>null</c>.</exception> |
| | | 78 | | public static ExpirationPolicy? Custom(Func<bool> isExpiredFunc, Action? resetAction = null, bool force = false) |
| | | 79 | | { |
| | 3 | 80 | | ArgumentNullException.ThrowIfNull(isExpiredFunc); |
| | | 81 | | |
| | 0 | 82 | | return force || !isExpiredFunc.Invoke() ? new CustomExpirationPolicy(isExpiredFunc, resetAction) : null; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Resets the expiration policy. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <exception cref="InvalidOperationException">If the policy do not support this operation.</exception> |
| | | 89 | | public void Reset() |
| | | 90 | | { |
| | 6 | 91 | | IsResting = true; |
| | | 92 | | |
| | | 93 | | try |
| | | 94 | | { |
| | 6 | 95 | | if (!CanReset) |
| | | 96 | | { |
| | 3 | 97 | | throw new InvalidOperationException("This policy can't be reset"); |
| | | 98 | | } |
| | | 99 | | |
| | 3 | 100 | | OnReset(); |
| | 3 | 101 | | } |
| | | 102 | | finally |
| | | 103 | | { |
| | 6 | 104 | | IsResting = false; |
| | 6 | 105 | | } |
| | 3 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Called when the policy is resetting. |
| | | 110 | | /// </summary> |
| | | 111 | | protected virtual void OnReset() |
| | | 112 | | { |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | #endregion |
| | | 116 | | } |
| | | 117 | | |