-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from Axemasta/feature/memory-cache
Add InMemory Cache Provider
- Loading branch information
Showing
14 changed files
with
385 additions
and
21 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
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
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,44 @@ | ||
using System.Globalization; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace Mocale.Cache; | ||
|
||
internal class InMemoryCacheManager(MemoryCache memoryCache) : ILocalisationCacheManager, ICacheUpdateManager | ||
{ | ||
public bool CanUpdateCache(CultureInfo cultureInfo) | ||
{ | ||
return !memoryCache.TryGetValue(cultureInfo, out _); | ||
} | ||
|
||
public void ClearCache(CultureInfo cultureInfo) | ||
{ | ||
memoryCache.Remove(cultureInfo); | ||
} | ||
|
||
public void ClearCache() | ||
{ | ||
memoryCache.Clear(); | ||
} | ||
|
||
public Dictionary<string, string>? GetCachedLocalizations(CultureInfo cultureInfo) | ||
{ | ||
if (memoryCache.TryGetValue<Dictionary<string, string>>(cultureInfo, out var cachedLocalizations)) | ||
{ | ||
return cachedLocalizations; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public bool SaveCachedLocalizations(CultureInfo cultureInfo, Dictionary<string, string> localizations) | ||
{ | ||
memoryCache.Set(cultureInfo, localizations); | ||
|
||
return true; | ||
} | ||
|
||
public bool SetCacheUpdated(CultureInfo cultureInfo) | ||
{ | ||
return true; | ||
} | ||
} |
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
148 changes: 148 additions & 0 deletions
148
tests/Mocale.UnitTests/Cache/InMemoryCacheManagerTests.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,148 @@ | ||
using System.Globalization; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Mocale.Cache; | ||
|
||
namespace Mocale.UnitTests.Cache; | ||
|
||
public class InMemoryCacheManagerTests : FixtureBase, IDisposable | ||
{ | ||
#region Setup | ||
|
||
private readonly MemoryCache memoryCache = new(new MemoryCacheOptions()); | ||
|
||
public override object CreateSystemUnderTest() | ||
{ | ||
return new InMemoryCacheManager(memoryCache); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
memoryCache?.Dispose(); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
#endregion Setup | ||
|
||
#region Tests | ||
|
||
[Fact] | ||
public void CanUpdateCache_WhenKeyNotInCache_ReturnsTrue() | ||
{ | ||
// Arrange | ||
var culture = new CultureInfo("en-US"); | ||
|
||
// Act | ||
var result = GetSut<InMemoryCacheManager>().CanUpdateCache(culture); | ||
|
||
// Assert | ||
Assert.True(result); | ||
} | ||
|
||
[Fact] | ||
public void CanUpdateCache_WhenKeyInCache_ReturnsFalse() | ||
{ | ||
// Arrange | ||
var culture = new CultureInfo("en-US"); | ||
memoryCache.Set(culture, new Dictionary<string, string>() | ||
{ | ||
{ "KeyOne", "ValueOne" } | ||
}); | ||
|
||
// Act | ||
var result = GetSut<InMemoryCacheManager>().CanUpdateCache(culture); | ||
|
||
// Assert | ||
Assert.False(result); | ||
} | ||
|
||
[Fact] | ||
public void ClearCache_RemovesCacheEntry() | ||
{ | ||
// Arrange | ||
var culture = new CultureInfo("en-US"); | ||
memoryCache.Set(culture, new Dictionary<string, string>()); | ||
|
||
// Act | ||
GetSut<InMemoryCacheManager>().ClearCache(culture); | ||
|
||
// Assert | ||
Assert.False(memoryCache.TryGetValue(culture, out _)); | ||
} | ||
|
||
[Fact] | ||
public void ClearCache_CallsClearMethod() | ||
{ | ||
// Arrange | ||
memoryCache.Set(new CultureInfo("en-US"), new Dictionary<string, string>()); | ||
memoryCache.Set(new CultureInfo("en-GB"), new Dictionary<string, string>()); | ||
memoryCache.Set(new CultureInfo("fr-FR"), new Dictionary<string, string>()); | ||
|
||
Assert.Equal(3, memoryCache.Count); | ||
|
||
// Act | ||
GetSut<InMemoryCacheManager>().ClearCache(); | ||
|
||
// Assert | ||
Assert.Equal(0, memoryCache.Count); | ||
} | ||
|
||
[Fact] | ||
public void GetCachedLocalizations_WhenInCache_ReturnsDictionary() | ||
{ | ||
// Arrange | ||
var culture = new CultureInfo("en-US"); | ||
var localizations = new Dictionary<string, string> { { "Hello", "Hola" } }; | ||
memoryCache.Set(culture, localizations); | ||
|
||
// Act | ||
var result = GetSut<InMemoryCacheManager>().GetCachedLocalizations(culture); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.Equal("Hola", result["Hello"]); | ||
} | ||
|
||
[Fact] | ||
public void GetCachedLocalizations_WhenNotInCache_ReturnsNull() | ||
{ | ||
// Arrange | ||
var culture = new CultureInfo("en-US"); | ||
|
||
// Act | ||
var result = GetSut<InMemoryCacheManager>().GetCachedLocalizations(culture); | ||
|
||
// Assert | ||
Assert.Null(result); | ||
} | ||
|
||
[Fact] | ||
public void SaveCachedLocalizations_AddsEntryToCache() | ||
{ | ||
// Arrange | ||
var culture = new CultureInfo("en-US"); | ||
var localizations = new Dictionary<string, string> { { "Hello", "Hola" } }; | ||
|
||
// Act | ||
var result = GetSut<InMemoryCacheManager>().SaveCachedLocalizations(culture, localizations); | ||
|
||
// Assert | ||
Assert.True(result); | ||
Assert.True(memoryCache.TryGetValue(culture, out var cachedData)); | ||
Assert.Equal(localizations, cachedData); | ||
} | ||
|
||
[Fact] | ||
public void SetCacheUpdated_AlwaysReturnsTrue() | ||
{ | ||
// Arrange | ||
var culture = new CultureInfo("en-US"); | ||
|
||
// Act | ||
var result = GetSut<InMemoryCacheManager>().SetCacheUpdated(culture); | ||
|
||
// Assert | ||
Assert.True(result); | ||
} | ||
|
||
#endregion Tests | ||
} |
Oops, something went wrong.