-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved from IMemoryCache to IDistributedCache, catering for multiple i… (
#1115) * Moved from IMemoryCache to IDistributedCache, catering for multiple intances of the api * Change store and category info cache to expire on an hourly basis ensuring new stores are picked-up
- Loading branch information
1 parent
45d067a
commit 63287f5
Showing
5 changed files
with
149 additions
and
40 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/api/src/domain/Yoma.Core.Domain/Core/Interfaces/IDistributedCacheService.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,15 @@ | ||
namespace Yoma.Core.Domain.Core.Interfaces | ||
{ | ||
public interface IDistributedCacheService | ||
{ | ||
T GetOrCreate<T>(string key, Func<T> valueProvider, TimeSpan? slidingExpiration = null, TimeSpan? absoluteExpirationRelativeToNow = null) | ||
where T : class; | ||
|
||
Task<T> GetOrCreateAsync<T>(string key, Func<Task<T>> valueProvider, TimeSpan? slidingExpiration = null, TimeSpan? absoluteExpirationRelativeToNow = null) | ||
where T : class; | ||
|
||
void Remove(string key); | ||
|
||
Task RemoveAsync(string key); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/api/src/domain/Yoma.Core.Domain/Core/Services/DistributedCacheService.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,94 @@ | ||
using Newtonsoft.Json; | ||
using StackExchange.Redis; | ||
using Yoma.Core.Domain.Core.Interfaces; | ||
|
||
namespace Yoma.Core.Domain.Core.Services | ||
{ | ||
public class DistributedCacheService : IDistributedCacheService | ||
{ | ||
#region Class Variables | ||
private readonly IDatabase _database; | ||
#endregion | ||
|
||
#region Constructor | ||
public DistributedCacheService(IConnectionMultiplexer connectionMultiplexer) | ||
{ | ||
_database = connectionMultiplexer.GetDatabase(); | ||
} | ||
#endregion | ||
|
||
#region Public Members | ||
public T GetOrCreate<T>(string key, Func<T> valueProvider, TimeSpan? slidingExpiration = null, TimeSpan? absoluteExpirationRelativeToNow = null) | ||
where T : class | ||
{ | ||
return GetOrCreateInternalAsync(key, () => Task.FromResult(valueProvider()), slidingExpiration, absoluteExpirationRelativeToNow).Result; | ||
} | ||
|
||
public async Task<T> GetOrCreateAsync<T>(string key, Func<Task<T>> valueProvider, TimeSpan? slidingExpiration = null, TimeSpan? absoluteExpirationRelativeToNow = null) | ||
where T : class | ||
{ | ||
return await GetOrCreateInternalAsync(key, valueProvider, slidingExpiration, absoluteExpirationRelativeToNow); | ||
} | ||
|
||
public void Remove(string key) | ||
{ | ||
RemoveInternalAsync(key).Wait(); | ||
} | ||
|
||
public async Task RemoveAsync(string key) | ||
{ | ||
await RemoveInternalAsync(key); | ||
} | ||
#endregion | ||
|
||
#region Private Members | ||
private async Task<T> GetOrCreateInternalAsync<T>(string key, Func<Task<T>> valueProvider, TimeSpan? slidingExpiration = null, TimeSpan? absoluteExpirationRelativeToNow = null) | ||
where T : class | ||
{ | ||
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key)); | ||
key = key.Trim(); | ||
|
||
ArgumentNullException.ThrowIfNull(valueProvider, nameof(valueProvider)); | ||
|
||
if (slidingExpiration.HasValue && absoluteExpirationRelativeToNow.HasValue && slidingExpiration > absoluteExpirationRelativeToNow) | ||
throw new InvalidOperationException("'Sliding Expiration' cannot be longer than 'Absolute Expiration Relative to Now'"); | ||
|
||
var redisValueWithExpiry = await _database.StringGetWithExpiryAsync(key); | ||
|
||
if (redisValueWithExpiry.Value.HasValue) | ||
{ | ||
var cachedValue = JsonConvert.DeserializeObject<T>(redisValueWithExpiry.Value!) | ||
?? throw new InvalidOperationException($"Failed to deserialize value for key '{key}'"); | ||
|
||
if (slidingExpiration.HasValue && redisValueWithExpiry.Expiry.HasValue) | ||
{ | ||
var newExpiration = slidingExpiration.Value < redisValueWithExpiry.Expiry.Value | ||
? slidingExpiration.Value | ||
: redisValueWithExpiry.Expiry.Value; | ||
|
||
await _database.KeyExpireAsync(key, newExpiration); | ||
} | ||
|
||
return cachedValue; | ||
} | ||
|
||
var value = await valueProvider(); | ||
var serializedValue = JsonConvert.SerializeObject(value); | ||
var expiration = absoluteExpirationRelativeToNow ?? slidingExpiration; | ||
|
||
await _database.StringSetAsync(key, serializedValue, expiration); | ||
|
||
return value; | ||
} | ||
|
||
private async Task RemoveInternalAsync(string key) | ||
{ | ||
ArgumentException.ThrowIfNullOrWhiteSpace(key, nameof(key)); | ||
key = key.Trim(); | ||
|
||
await _database.KeyDeleteAsync(key); | ||
} | ||
} | ||
#endregion | ||
} | ||
|
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