< Summary

Information
Class: MyNet.Utilities.Caching.Policies.ExpirationPolicy
Assembly: MyNet.Utilities
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Utilities/Caching/Policies/ExpirationPolicy.cs
Tag: 323_28699572109
Line coverage
85%
Covered lines: 12
Uncovered lines: 2
Coverable lines: 14
Total lines: 117
Line coverage: 85.7%
Branch coverage
66%
Covered branches: 12
Total branches: 18
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Absolute(...)100%44100%
Duration(...)100%44100%
Sliding(...)50%44100%
Custom(...)0%6450%
Reset()100%22100%
OnReset()100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Utilities/Caching/Policies/ExpirationPolicy.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ExpirationPolicy.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8
 9namespace 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>
 18public 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)
 948        => 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)
 2158        => 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)
 368        => 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    {
 380        ArgumentNullException.ThrowIfNull(isExpiredFunc);
 81
 082        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    {
 691        IsResting = true;
 92
 93        try
 94        {
 695            if (!CanReset)
 96            {
 397                throw new InvalidOperationException("This policy can't be reset");
 98            }
 99
 3100            OnReset();
 3101        }
 102        finally
 103        {
 6104            IsResting = false;
 6105        }
 3106    }
 107
 108    /// <summary>
 109    /// Called when the policy is resetting.
 110    /// </summary>
 111    protected virtual void OnReset()
 112    {
 0113    }
 114
 115    #endregion
 116}
 117