Created
October 14, 2020 12:34
-
-
Save umayeras/58c2f012bbf41c10545401861f9a3eef to your computer and use it in GitHub Desktop.
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
private IConfiguration Configuration { get; } | |
private readonly ILogger<CachingService> logger; | |
public CachingService(IConfiguration configuration,ILogger<CachingService> logger) | |
{ | |
Configuration = configuration; | |
this.logger = logger; | |
} | |
private RedisCacheProviderStatus ServiceStatus() | |
{ | |
try | |
{ | |
var redisSettings = Configuration.GetSection(SectionNames.RedisSettings).Get<RedisSettings>(); | |
var manager = new RedisManagerPool(redisSettings.Uri); | |
using var client = manager.GetClient(); | |
var isClientAvailable = client.Ping(); | |
return !isClientAvailable | |
? RedisCacheProviderStatus.NotAvailable | |
: RedisCacheProviderStatus.Available; | |
} | |
catch (Exception ex) | |
{ | |
logger.LogError(nameof(ServiceStatus), ex.Message); | |
return RedisCacheProviderStatus.NotAvailable; | |
} | |
} | |
public T Get<T>(string key) | |
{ | |
var serviceStatus = ServiceStatus(); | |
if (serviceStatus == RedisCacheProviderStatus.NotAvailable) | |
{ | |
return JsonConvert.DeserializeObject<T>(string.Empty); | |
} | |
using var client = new RedisClient(); | |
var cachedDataString = client.GetValue(key); | |
return JsonConvert.DeserializeObject<T>(cachedDataString); | |
} | |
public void Set(string key, object value) | |
{ | |
var serviceStatus = ServiceStatus(); | |
if (serviceStatus == RedisCacheProviderStatus.NotAvailable) | |
{ | |
return; | |
} | |
using var client = new RedisClient(); | |
var keyExists = Exists(key); | |
if (keyExists) | |
{ | |
return; | |
} | |
client.Set(key, value); | |
} | |
public void Delete(string key) | |
{ | |
var serviceStatus = ServiceStatus(); | |
if (serviceStatus == RedisCacheProviderStatus.NotAvailable) | |
{ | |
return; | |
} | |
using var client = new RedisClient(); | |
client.Delete(key); | |
} | |
public void Refresh(string key, object value) | |
{ | |
var serviceStatus = ServiceStatus(); | |
if (serviceStatus == RedisCacheProviderStatus.NotAvailable) | |
{ | |
return; | |
} | |
Delete(key); | |
Set(key, value); | |
} | |
public bool Exists(string key) | |
{ | |
var serviceStatus = ServiceStatus(); | |
if (serviceStatus == RedisCacheProviderStatus.NotAvailable) | |
{ | |
return false; | |
} | |
using var client = new RedisClient(); | |
return client.SearchKeys(key).Count > 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment