-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
216 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/EFCoreSecondLevelCacheInterceptor/EFCacheServiceCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace EFCoreSecondLevelCacheInterceptor; | ||
|
||
/// <summary> | ||
/// Is the configured cache provider online? | ||
/// </summary> | ||
public class EFCacheServiceCheck : IEFCacheServiceCheck | ||
{ | ||
private readonly IEFCacheServiceProvider _cacheServiceProvider; | ||
private readonly EFCoreSecondLevelCacheSettings _cacheSettings; | ||
|
||
private bool? _isCacheServerAvailable; | ||
private DateTime? _lastCheckTime; | ||
|
||
/// <summary> | ||
/// Is the configured cache provider online? | ||
/// </summary> | ||
public EFCacheServiceCheck(IOptions<EFCoreSecondLevelCacheSettings> cacheSettings, | ||
IEFCacheServiceProvider cacheServiceProvider) | ||
{ | ||
if (cacheSettings == null) | ||
{ | ||
throw new ArgumentNullException(nameof(cacheSettings)); | ||
} | ||
|
||
_cacheServiceProvider = cacheServiceProvider; | ||
_cacheSettings = cacheSettings.Value; | ||
} | ||
|
||
/// <summary> | ||
/// Is the configured cache services online and available? Can we use it without any problem? | ||
/// </summary> | ||
public bool IsCacheServiceAvailable() | ||
{ | ||
if (!_cacheSettings.UseDbCallsIfCachingProviderIsDown) | ||
{ | ||
return true; | ||
} | ||
|
||
var now = DateTime.UtcNow; | ||
|
||
if (_lastCheckTime.HasValue && | ||
_isCacheServerAvailable.HasValue && | ||
now - _lastCheckTime.Value < _cacheSettings.NextCacheServerAvailabilityCheck) | ||
{ | ||
return _isCacheServerAvailable.Value; | ||
} | ||
|
||
try | ||
{ | ||
_lastCheckTime = now; | ||
_ = _cacheServiceProvider.GetValue(new EFCacheKey(new HashSet<string>(StringComparer.OrdinalIgnoreCase) | ||
{ "__Name__" }) { KeyHash = "__Test__" }, | ||
new EFCachePolicy()); | ||
_isCacheServerAvailable = true; | ||
} | ||
catch | ||
{ | ||
_isCacheServerAvailable = false; | ||
if (_cacheSettings.UseDbCallsIfCachingProviderIsDown) | ||
{ | ||
throw; | ||
} | ||
} | ||
|
||
return _isCacheServerAvailable.Value; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/EFCoreSecondLevelCacheInterceptor/EFCoreSecondLevelCacheInterceptor.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/EFCoreSecondLevelCacheInterceptor/IEFCacheServiceCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace EFCoreSecondLevelCacheInterceptor; | ||
|
||
/// <summary> | ||
/// Is the configured cache provider online? | ||
/// </summary> | ||
public interface IEFCacheServiceCheck | ||
{ | ||
/// <summary> | ||
/// Is the configured cache services online and available? Can we use it without any problem? | ||
/// </summary> | ||
bool IsCacheServiceAvailable(); | ||
} |
Oops, something went wrong.