| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="CustomExpirationPolicy.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 custom expiration policy. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// Initializes a new instance of the <see cref="CustomExpirationPolicy"/> class. |
| | | 16 | | /// </remarks> |
| | | 17 | | /// <param name="isExpiredFunc"> |
| | | 18 | | /// The function to check if the policy is expired. |
| | | 19 | | /// </param> |
| | | 20 | | /// <param name="resetAction"> |
| | | 21 | | /// The action that will be executed if the item is read before expiration. |
| | | 22 | | /// </param> |
| | 0 | 23 | | public sealed class CustomExpirationPolicy(Func<bool> isExpiredFunc, Action? resetAction = null) : ExpirationPolicy(rese |
| | | 24 | | { |
| | | 25 | | #region Fields |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The function to check if the policy is expired. |
| | | 29 | | /// </summary> |
| | 0 | 30 | | private readonly Func<bool> _isExpiredFunc = isExpiredFunc ?? throw new ArgumentNullException(nameof(isExpiredFunc)) |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// The action that will be executed if the item is read before expiration. |
| | | 34 | | /// </summary> |
| | 0 | 35 | | private readonly Action? _resetAction = resetAction; |
| | | 36 | | |
| | | 37 | | #endregion |
| | | 38 | | |
| | | 39 | | #region Properties |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets a value indicating whether is expired. |
| | | 43 | | /// </summary> |
| | 0 | 44 | | public override bool IsExpired => _isExpiredFunc.Invoke(); |
| | | 45 | | #endregion |
| | | 46 | | |
| | | 47 | | #region Methods |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Called when the policy is resetting. |
| | | 51 | | /// </summary> |
| | 0 | 52 | | protected override void OnReset() => _resetAction?.Invoke(); |
| | | 53 | | |
| | | 54 | | #endregion |
| | | 55 | | } |
| | | 56 | | |