/// <summary> /// Reset the cache expiry. /// </summary> public void Reset() { Hits = 0; // Where the expiry is not set then set. DateTime now = DateTime.Now; if (Expiry == null) { DateTime time = now.Date; while (time < now) { time = time.Add(_duration); if (time.DayOfYear != now.DayOfYear) { time = now.Date.AddDays(1); } } Expiry = CachePolicyManager.AddRandomizedOffsetToTime(time, _randomizerOffset); return; } // Add duration; if result is tomorrow reset to start of day tomorrow plus offset. DateTime temp = now.Add(_duration); if (temp.DayOfYear != Expiry.Value.DayOfYear) { Expiry = CachePolicyManager.AddRandomizedOffsetToTime(temp, _randomizerOffset); } else { Expiry = temp; } }
/// <summary> /// Reset the cache expiry. /// </summary> public void Reset() { DateTime now = Entities.Cleaner.Clean(DateTime.Now); DateTime?expiry = now.Add(_duration); if (_randomizerOffset.HasValue) { expiry = CachePolicyManager.AddRandomizedOffsetToTime(expiry.Value, _randomizerOffset.Value); } Expiry = expiry; Hits = 0; }
/// <summary> /// Sets (configures) the <see cref="CachePolicyManager"/> using the <paramref name="config"/>. /// </summary> /// <param name="manager">The <see cref="CachePolicyManager"/>.</param> /// <param name="config">The <see cref="CachePolicyConfig"/>.</param> internal static void SetCachePolicyManager(CachePolicyManager manager, CachePolicyConfig config) { if (config == null) { return; } // Load the policies. var isDefaultSet = false; if (config.Policies == null) { return; } foreach (var pol in config.Policies) { if (string.IsNullOrEmpty(pol.Name)) { throw new CachePolicyConfigException("A Policy has been defined with no Name."); } if (string.IsNullOrEmpty(pol.Policy)) { throw new CachePolicyConfigException($"Policy '{pol.Name}' has been defined with no Type."); } try { var type = Type.GetType(pol.Policy) ?? throw new CachePolicyConfigException($"Policy '{pol.Name}' Type '{pol.Policy}' could not be loaded/instantiated."); var policy = (ICachePolicy)Activator.CreateInstance(type); LoadPolicyProperties(pol, type, policy); LoadCaches(manager, pol, policy); if (pol.IsDefault) { if (isDefaultSet) { throw new CachePolicyConfigException($"Policy '{pol.Name}' can not set DefaultPolicy where already set."); } isDefaultSet = true; manager.DefaultPolicy = policy; } } catch (CachePolicyConfigException) { throw; } catch (Exception ex) { throw new CachePolicyConfigException($"Policy '{pol.Name}' Type '{pol.Policy}' could not be loaded/instantiated: {ex.Message}", ex); } } }
/// <summary> /// Loads the cache type and policy configurations. /// </summary> private static void LoadCaches(CachePolicyConfigPolicy config, ICachePolicy policy) { if (config.Caches == null) { return; } foreach (var cache in config.Caches) { if (!string.IsNullOrEmpty(cache)) { CachePolicyManager.Set(cache, policy); } } }
/// <summary> /// Reset the cache expiry. /// </summary> public void Reset() { DateTime now = DateTime.Now; DateTime?expiry = now.Add(_duration); if (!_maxExpiry.HasValue && _maxDuration.TotalMilliseconds > 0) { _maxExpiry = now.Add(_maxDuration); } if (_maxExpiry.HasValue && Expiry > _maxExpiry.Value) { expiry = _maxExpiry; } if (_randomizerOffset.HasValue) { expiry = CachePolicyManager.AddRandomizedOffsetToTime(expiry.Value, _randomizerOffset.Value); } Expiry = expiry; Hits = 0; }
/// <summary> /// Initializes a new instance of the <see cref="CachePolicyManagerServiceHost"/> class. /// </summary> /// <param name="cachePolicyManager">The <see cref="CachePolicyManager"/>.</param> /// <param name="serviceProvider">The <see cref="IServiceProvider"/>.</param> /// <param name="logger">The <see cref="ILogger"/>.</param> /// <param name="config">The <see cref="IConfiguration"/>; defaults to instance from the <paramref name="serviceProvider"/> where not specified.</param> public CachePolicyManagerServiceHost(CachePolicyManager cachePolicyManager, IServiceProvider serviceProvider, ILogger logger, IConfiguration?config = null) : base(serviceProvider, logger, config) => _cachePolicyManager = Check.NotNull(cachePolicyManager, nameof(cachePolicyManager));