Skip to content

Commit 476840e

Browse files
committed
Add new option IgnoreTimeoutsWhenDebugging.
No tests because, well, I think it cannot be tested for real.
1 parent 85d814c commit 476840e

File tree

6 files changed

+41
-9
lines changed

6 files changed

+41
-9
lines changed

src/ZiggyCreatures.FusionCache/FusionCacheEntryOptions.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel;
2+
using System.Diagnostics;
23
using Microsoft.Extensions.Caching.Distributed;
34
using Microsoft.Extensions.Caching.Memory;
45
using Microsoft.Extensions.Logging;
@@ -1017,8 +1018,14 @@ internal DistributedCacheEntryOptions ToDistributedCacheEntryOptions(FusionCache
10171018
return res;
10181019
}
10191020

1020-
internal TimeSpan GetAppropriateMemoryLockTimeout(bool hasFallbackValue)
1021+
internal TimeSpan GetAppropriateMemoryLockTimeout(FusionCacheOptions options, bool hasFallbackValue)
10211022
{
1023+
// EARLY RETURN: IGNORE TIMEOUTS WHEN DEBUGGING
1024+
if (options.IgnoreTimeoutsWhenDebugging && Debugger.IsAttached)
1025+
{
1026+
return Timeout.InfiniteTimeSpan;
1027+
}
1028+
10221029
var res = LockTimeout;
10231030
if (res == Timeout.InfiniteTimeSpan && hasFallbackValue && IsFailSafeEnabled && FactorySoftTimeout != Timeout.InfiniteTimeSpan)
10241031
{
@@ -1033,8 +1040,14 @@ internal TimeSpan GetAppropriateMemoryLockTimeout(bool hasFallbackValue)
10331040
return res;
10341041
}
10351042

1036-
internal TimeSpan GetAppropriateFactoryTimeout(bool hasFallbackValue)
1043+
internal TimeSpan GetAppropriateFactoryTimeout(FusionCacheOptions options, bool hasFallbackValue)
10371044
{
1045+
// EARLY RETURN: IGNORE TIMEOUTS WHEN DEBUGGING
1046+
if (options.IgnoreTimeoutsWhenDebugging && Debugger.IsAttached)
1047+
{
1048+
return Timeout.InfiniteTimeSpan;
1049+
}
1050+
10381051
// EARLY RETURN: WHEN NO TIMEOUTS AT ALL
10391052
if (FactorySoftTimeout == Timeout.InfiniteTimeSpan && FactoryHardTimeout == Timeout.InfiniteTimeSpan)
10401053
return Timeout.InfiniteTimeSpan;
@@ -1054,8 +1067,14 @@ internal TimeSpan GetAppropriateFactoryTimeout(bool hasFallbackValue)
10541067
return res;
10551068
}
10561069

1057-
internal TimeSpan GetAppropriateDistributedCacheTimeout(bool hasFallbackValue)
1070+
internal TimeSpan GetAppropriateDistributedCacheTimeout(FusionCacheOptions options, bool hasFallbackValue)
10581071
{
1072+
// EARLY RETURN: IGNORE TIMEOUTS WHEN DEBUGGING
1073+
if (options.IgnoreTimeoutsWhenDebugging && Debugger.IsAttached)
1074+
{
1075+
return Timeout.InfiniteTimeSpan;
1076+
}
1077+
10591078
// EARLY RETURN: WHEN NO TIMEOUTS AT ALL
10601079
if (DistributedCacheSoftTimeout == Timeout.InfiniteTimeSpan && DistributedCacheHardTimeout == Timeout.InfiniteTimeSpan)
10611080
return Timeout.InfiniteTimeSpan;

src/ZiggyCreatures.FusionCache/FusionCacheOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public FusionCacheOptions()
9191

9292
SkipAutoCloneForImmutableObjects = true;
9393

94+
IgnoreTimeoutsWhenDebugging = false;
95+
9496
// AUTO-RECOVERY
9597
EnableAutoRecovery = true;
9698
AutoRecoveryMaxItems = null;
@@ -458,6 +460,15 @@ public TimeSpan BackplaneAutoRecoveryDelay
458460
/// </summary>
459461
public bool DisableTagging { get; set; }
460462

463+
/// <summary>
464+
/// If set to <see langword="true"/>, ignores the timeouts when debugging, meaning when there's a debugger attached (check done via <see cref="Debugger.IsAttached"/>).
465+
/// <br/>
466+
/// All the available timeouts are supported: factory timeouts (soft + hard), distributed cache timeouts (soft + hard), lock timeout, etc.
467+
/// <br/><br/>
468+
/// <strong>DOCS:</strong> <see href="https://github.com/ZiggyCreatures/FusionCache/blob/main/docs/Timeouts.md"/>
469+
/// </summary>
470+
public bool IgnoreTimeoutsWhenDebugging { get; set; }
471+
461472
/// <summary>
462473
/// Specify the <see cref="LogLevel"/> to use when some options have incoherent values that have been fixed with a normalization, like for example when a FailSafeMaxDuration is lower than a Duration, so the Duration is used instead.
463474
/// <br/><br/>
@@ -606,6 +617,8 @@ public FusionCacheOptions Duplicate()
606617

607618
DisableTagging = DisableTagging,
608619

620+
IgnoreTimeoutsWhenDebugging = IgnoreTimeoutsWhenDebugging,
621+
609622
SkipAutoCloneForImmutableObjects = SkipAutoCloneForImmutableObjects,
610623

611624
// LOG LEVELS

src/ZiggyCreatures.FusionCache/FusionCache_Async.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private void ExecuteEagerRefreshWithAsyncFactory<TValue>(string operationId, str
144144
try
145145
{
146146
// MEMORY LOCK
147-
memoryLockObj = await AcquireMemoryLockAsync(operationId, key, options.GetAppropriateMemoryLockTimeout(memoryEntry is not null), token).ConfigureAwait(false);
147+
memoryLockObj = await AcquireMemoryLockAsync(operationId, key, options.GetAppropriateMemoryLockTimeout(_options, memoryEntry is not null), token).ConfigureAwait(false);
148148

149149
if (memoryLockObj is null && options.IsFailSafeEnabled && memoryEntry is not null)
150150
{
@@ -223,7 +223,7 @@ private void ExecuteEagerRefreshWithAsyncFactory<TValue>(string operationId, str
223223
{
224224
Task<TValue>? factoryTask = null;
225225

226-
var timeout = options.GetAppropriateFactoryTimeout(memoryEntry is not null || distributedEntry is not null);
226+
var timeout = options.GetAppropriateFactoryTimeout(_options, memoryEntry is not null || distributedEntry is not null);
227227

228228
if (_logger?.IsEnabled(LogLevel.Debug) ?? false)
229229
_logger.Log(LogLevel.Debug, "FUSION [N={CacheName} I={CacheInstanceId}] (O={CacheOperationId} K={CacheKey}): calling the factory (timeout={Timeout})", CacheName, InstanceId, operationId, key, timeout.ToLogString_Timeout());

src/ZiggyCreatures.FusionCache/FusionCache_Sync.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private void ExecuteEagerRefreshWithSyncFactory<TValue>(string operationId, stri
144144
try
145145
{
146146
// MEMORY LOCK
147-
memoryLockObj = AcquireMemoryLock(operationId, key, options.GetAppropriateMemoryLockTimeout(memoryEntry is not null), token);
147+
memoryLockObj = AcquireMemoryLock(operationId, key, options.GetAppropriateMemoryLockTimeout(_options, memoryEntry is not null), token);
148148

149149
if (memoryLockObj is null && options.IsFailSafeEnabled && memoryEntry is not null)
150150
{
@@ -223,7 +223,7 @@ private void ExecuteEagerRefreshWithSyncFactory<TValue>(string operationId, stri
223223
{
224224
Task<TValue>? factoryTask = null;
225225

226-
var timeout = options.GetAppropriateFactoryTimeout(memoryEntry is not null || distributedEntry is not null);
226+
var timeout = options.GetAppropriateFactoryTimeout(_options, memoryEntry is not null || distributedEntry is not null);
227227

228228
if (_logger?.IsEnabled(LogLevel.Debug) ?? false)
229229
_logger.Log(LogLevel.Debug, "FUSION [N={CacheName} I={CacheInstanceId}] (O={CacheOperationId} K={CacheKey}): calling the factory (timeout={Timeout})", CacheName, InstanceId, operationId, key, timeout.ToLogString_Timeout());

src/ZiggyCreatures.FusionCache/Internals/Distributed/DistributedCacheAccessor_Async.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public async ValueTask<bool> SetEntryAsync<TValue>(string operationId, string ke
157157
byte[]? data;
158158
try
159159
{
160-
timeout ??= options.GetAppropriateDistributedCacheTimeout(hasFallbackValue);
160+
timeout ??= options.GetAppropriateDistributedCacheTimeout(_options, hasFallbackValue);
161161
data = await RunUtils.RunAsyncFuncWithTimeoutAsync<byte[]?>(
162162
async ct => await _cache.GetAsync(MaybeProcessCacheKey(key), ct).ConfigureAwait(false),
163163
timeout.Value,

src/ZiggyCreatures.FusionCache/Internals/Distributed/DistributedCacheAccessor_Sync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public bool SetEntry<TValue>(string operationId, string key, IFusionCacheEntry e
150150
byte[]? data;
151151
try
152152
{
153-
timeout ??= options.GetAppropriateDistributedCacheTimeout(hasFallbackValue);
153+
timeout ??= options.GetAppropriateDistributedCacheTimeout(_options, hasFallbackValue);
154154
data = RunUtils.RunSyncFuncWithTimeout<byte[]?>(
155155
_ => _cache.Get(MaybeProcessCacheKey(key)),
156156
timeout.Value,

0 commit comments

Comments
 (0)