From 4955bc4c1e2dfdbdf0c9cb2dc495b9392d8e47af Mon Sep 17 00:00:00 2001 From: Anna Kolesnyk Date: Sun, 24 Sep 2023 12:25:34 +0300 Subject: [PATCH 1/2] fix: add cache to GetContentAsync --- src/Joystick.Client/JoystickClient.cs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Joystick.Client/JoystickClient.cs b/src/Joystick.Client/JoystickClient.cs index bd242d6..6045c15 100644 --- a/src/Joystick.Client/JoystickClient.cs +++ b/src/Joystick.Client/JoystickClient.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Net.Http; @@ -99,11 +100,12 @@ public JoystickClient( public async Task> GetContentsAsync(IEnumerable contentIds, JoystickContentOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { var settings = new GetContentSettings(options, typeof(TData)); - var serializedContents = await this.GetSerializedFullContentsAsync(contentIds, settings, cancellationToken); + var enumerable = contentIds as string[] ?? contentIds.ToArray(); + var serializedContents = await this.GetSerializedFullContentsAsync(enumerable, settings, cancellationToken); var contentDataDictionary = this.contentSerializer.Deserialize>>(serializedContents); var result = new Dictionary(); - foreach (var contentId in contentIds) + foreach (var contentId in enumerable) { result[contentId] = contentDataDictionary[contentId].Data; } @@ -141,19 +143,32 @@ public void ClearCache() throw new JoystickArgumentException($"{nameof(contentId)} can't be empty."); } - var contentsJson = await this.httpService.GetJsonContentsAsync(new[] { contentId }, settings, cancellationToken); + var contentIds = new[] { contentId }; + + var cacheKay = CacheHelper.GenerateCacheKey(this.config, settings.IsContentSerialized, contentIds); + var isContainedInCache = this.cacheService.TryGet(cacheKay, out var contentsJson); + + if (!isContainedInCache || settings.Refresh == true) + { + contentsJson = await this.httpService.GetJsonContentsAsync(contentIds, settings, cancellationToken); + } var partiallyDeserialized = new Dictionary(StringComparer.OrdinalIgnoreCase); JsonConvert.PopulateObject(contentsJson, partiallyDeserialized); - JsonContentsValidator.Validate(partiallyDeserialized); + if (!isContainedInCache || settings.Refresh == true) + { + JsonContentsValidator.Validate(partiallyDeserialized); + this.cacheService.Set(cacheKay, contentsJson); + } + return partiallyDeserialized[contentId].ToString(); } private async Task GetSerializedFullContentsAsync(IEnumerable contentIds, GetContentSettings settings, CancellationToken cancellationToken = default(CancellationToken)) { var enumerable = contentIds?.ToList(); - if (contentIds == null || !enumerable.Any()) + if (enumerable == null || !enumerable.Any()) { throw new JoystickArgumentException($"{nameof(contentIds)} can't be empty."); } From a760d960895b8687bcd49399076fa4764a44449e Mon Sep 17 00:00:00 2001 From: Valerii Maslenykov Date: Thu, 28 Sep 2023 13:54:17 +0200 Subject: [PATCH 2/2] cacheKay -> cacheKey --- src/Joystick.Client/JoystickClient.cs | 12 ++++++------ tests/Joystick.UnitTests/Utils/CacheHelperTests.cs | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Joystick.Client/JoystickClient.cs b/src/Joystick.Client/JoystickClient.cs index 6045c15..4d37bb0 100644 --- a/src/Joystick.Client/JoystickClient.cs +++ b/src/Joystick.Client/JoystickClient.cs @@ -145,8 +145,8 @@ public void ClearCache() var contentIds = new[] { contentId }; - var cacheKay = CacheHelper.GenerateCacheKey(this.config, settings.IsContentSerialized, contentIds); - var isContainedInCache = this.cacheService.TryGet(cacheKay, out var contentsJson); + var cacheKey = CacheHelper.GenerateCacheKey(this.config, settings.IsContentSerialized, contentIds); + var isContainedInCache = this.cacheService.TryGet(cacheKey, out var contentsJson); if (!isContainedInCache || settings.Refresh == true) { @@ -159,7 +159,7 @@ public void ClearCache() if (!isContainedInCache || settings.Refresh == true) { JsonContentsValidator.Validate(partiallyDeserialized); - this.cacheService.Set(cacheKay, contentsJson); + this.cacheService.Set(cacheKey, contentsJson); } return partiallyDeserialized[contentId].ToString(); @@ -178,9 +178,9 @@ public void ClearCache() throw new JoystickArgumentException($"{nameof(contentIds)} can't contain empty value."); } - var cacheKay = CacheHelper.GenerateCacheKey(this.config, settings.IsContentSerialized, enumerable); + var cacheKey = CacheHelper.GenerateCacheKey(this.config, settings.IsContentSerialized, enumerable); - if (this.cacheService.TryGet(cacheKay, out var contentsJson) && settings.Refresh != true) + if (this.cacheService.TryGet(cacheKey, out var contentsJson) && settings.Refresh != true) { return contentsJson; } @@ -192,7 +192,7 @@ public void ClearCache() JsonContentsValidator.Validate(partiallyDeserialized); - this.cacheService.Set(cacheKay, contentsJson); + this.cacheService.Set(cacheKey, contentsJson); return contentsJson; } diff --git a/tests/Joystick.UnitTests/Utils/CacheHelperTests.cs b/tests/Joystick.UnitTests/Utils/CacheHelperTests.cs index b0fe716..40875b8 100644 --- a/tests/Joystick.UnitTests/Utils/CacheHelperTests.cs +++ b/tests/Joystick.UnitTests/Utils/CacheHelperTests.cs @@ -168,9 +168,9 @@ public void BuildStringCacheKey_Should_ReturnCorrectString() var isContentSerialized = true; var contentIds = new[] { "Auth", "Design" }; - var actualCacheKay = CacheHelper.BuildStringCacheKey(config, isContentSerialized, contentIds); + var actualCacheKey = CacheHelper.BuildStringCacheKey(config, isContentSerialized, contentIds); - Assert.Equal(expectedCacheKey, actualCacheKay); + Assert.Equal(expectedCacheKey, actualCacheKey); } } }