Skip to content

Commit

Permalink
Add EnableCachingInterceptor method
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Apr 5, 2024
1 parent b549903 commit 658ceb4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 16 additions & 5 deletions src/EFCoreSecondLevelCacheInterceptor/EFCacheServiceCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class EFCacheServiceCheck : IEFCacheServiceCheck
/// Is the configured cache provider online?
/// </summary>
public EFCacheServiceCheck(IOptions<EFCoreSecondLevelCacheSettings> cacheSettings,
IEFCacheServiceProvider cacheServiceProvider)
IEFCacheServiceProvider cacheServiceProvider)
{
if (cacheSettings == null)
{
Expand All @@ -35,15 +35,19 @@ public EFCacheServiceCheck(IOptions<EFCoreSecondLevelCacheSettings> cacheSetting
/// </summary>
public bool IsCacheServiceAvailable()
{
if (!_cacheSettings.IsCachingInterceptorEnabled)
{
return false;
}

if (!_cacheSettings.UseDbCallsIfCachingProviderIsDown)
{
return true;
}

var now = DateTime.UtcNow;

if (_lastCheckTime.HasValue &&
_isCacheServerAvailable.HasValue &&
if (_lastCheckTime.HasValue && _isCacheServerAvailable.HasValue &&
now - _lastCheckTime.Value < _cacheSettings.NextCacheServerAvailabilityCheck)
{
return _isCacheServerAvailable.Value;
Expand All @@ -52,14 +56,21 @@ public bool IsCacheServiceAvailable()
try
{
_lastCheckTime = now;

_ = _cacheServiceProvider.GetValue(new EFCacheKey(new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{ "__Name__" }) { KeyHash = "__Test__" },
new EFCachePolicy());
{
"__Name__"
})
{
KeyHash = "__Test__"
}, new EFCachePolicy());

_isCacheServerAvailable = true;
}
catch
{
_isCacheServerAvailable = false;

if (_cacheSettings.UseDbCallsIfCachingProviderIsDown)
{
throw;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,17 @@ public EFCoreSecondLevelCacheOptions UseDbCallsIfCachingProviderIsDown(TimeSpan
return this;
}

/// <summary>
/// Set it to false to disable this caching interceptor entirely.
/// Its default value is `true`.
/// </summary>
public EFCoreSecondLevelCacheOptions EnableCachingInterceptor(bool enable = true)
{
Settings.IsCachingInterceptorEnabled = true;

return this;
}

/// <summary>
/// Possibility to allow caching with explicit transactions.
/// Its default value is false.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public class EFCoreSecondLevelCacheSettings
/// </summary>
public bool UseDbCallsIfCachingProviderIsDown { set; get; }

/// <summary>
/// Set it to false to disable this caching interceptor.
/// Its default value is `true`.
/// </summary>
public bool IsCachingInterceptorEnabled { set; get; } = true;

/// <summary>
/// The cache server's availability check interval value.
/// </summary>
Expand Down

0 comments on commit 658ceb4

Please sign in to comment.