diff --git a/README.md b/README.md index ecfd7c1..77c834d 100644 --- a/README.md +++ b/README.md @@ -560,6 +560,12 @@ namespace EFCoreSecondLevelCacheInterceptor.AspNetCoreSample // ... ``` + +## Disabling the interceptor for a while + +If you want to disable this interceptor for a while, use the `.EnableCachingInterceptor(enable: false)` method. Its default value is true. + + ## Does it work?! You should enable the logging system to see the behind the scene of the caching interceptor. diff --git a/src/EFCoreSecondLevelCacheInterceptor/EFCacheServiceCheck.cs b/src/EFCoreSecondLevelCacheInterceptor/EFCacheServiceCheck.cs index 4aadce5..c7b87ca 100644 --- a/src/EFCoreSecondLevelCacheInterceptor/EFCacheServiceCheck.cs +++ b/src/EFCoreSecondLevelCacheInterceptor/EFCacheServiceCheck.cs @@ -19,7 +19,7 @@ public class EFCacheServiceCheck : IEFCacheServiceCheck /// Is the configured cache provider online? /// public EFCacheServiceCheck(IOptions cacheSettings, - IEFCacheServiceProvider cacheServiceProvider) + IEFCacheServiceProvider cacheServiceProvider) { if (cacheSettings == null) { @@ -35,6 +35,11 @@ public EFCacheServiceCheck(IOptions cacheSetting /// public bool IsCacheServiceAvailable() { + if (!_cacheSettings.IsCachingInterceptorEnabled) + { + return false; + } + if (!_cacheSettings.UseDbCallsIfCachingProviderIsDown) { return true; @@ -42,8 +47,7 @@ public bool IsCacheServiceAvailable() var now = DateTime.UtcNow; - if (_lastCheckTime.HasValue && - _isCacheServerAvailable.HasValue && + if (_lastCheckTime.HasValue && _isCacheServerAvailable.HasValue && now - _lastCheckTime.Value < _cacheSettings.NextCacheServerAvailabilityCheck) { return _isCacheServerAvailable.Value; @@ -52,14 +56,21 @@ public bool IsCacheServiceAvailable() try { _lastCheckTime = now; + _ = _cacheServiceProvider.GetValue(new EFCacheKey(new HashSet(StringComparer.OrdinalIgnoreCase) - { "__Name__" }) { KeyHash = "__Test__" }, - new EFCachePolicy()); + { + "__Name__" + }) + { + KeyHash = "__Test__" + }, new EFCachePolicy()); + _isCacheServerAvailable = true; } catch { _isCacheServerAvailable = false; + if (_cacheSettings.UseDbCallsIfCachingProviderIsDown) { throw; diff --git a/src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheOptions.cs b/src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheOptions.cs index 57071c1..93b5a21 100644 --- a/src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheOptions.cs +++ b/src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheOptions.cs @@ -347,6 +347,17 @@ public EFCoreSecondLevelCacheOptions UseDbCallsIfCachingProviderIsDown(TimeSpan return this; } + /// + /// Set it to false to disable this caching interceptor entirely. + /// Its default value is `true`. + /// + public EFCoreSecondLevelCacheOptions EnableCachingInterceptor(bool enable = true) + { + Settings.IsCachingInterceptorEnabled = true; + + return this; + } + /// /// Possibility to allow caching with explicit transactions. /// Its default value is false. diff --git a/src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheSettings.cs b/src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheSettings.cs index 6081d79..be48e59 100644 --- a/src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheSettings.cs +++ b/src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheSettings.cs @@ -76,6 +76,12 @@ public class EFCoreSecondLevelCacheSettings /// public bool UseDbCallsIfCachingProviderIsDown { set; get; } + /// + /// Set it to false to disable this caching interceptor. + /// Its default value is `true`. + /// + public bool IsCachingInterceptorEnabled { set; get; } = true; + /// /// The cache server's availability check interval value. ///