diff --git a/src/Joystick.Client/JoystickClient.cs b/src/Joystick.Client/JoystickClient.cs index bd242d6..4d37bb0 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 cacheKey = CacheHelper.GenerateCacheKey(this.config, settings.IsContentSerialized, contentIds); + var isContainedInCache = this.cacheService.TryGet(cacheKey, 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(cacheKey, 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."); } @@ -163,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; } @@ -177,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); } } }