Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/Joystick.Client/JoystickClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -99,11 +100,12 @@ public JoystickClient(
public async Task<Dictionary<string, TData>> GetContentsAsync<TData>(IEnumerable<string> 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<Dictionary<string, JoystickBaseContent<TData>>>(serializedContents);
var result = new Dictionary<string, TData>();
foreach (var contentId in contentIds)
foreach (var contentId in enumerable)
{
result[contentId] = contentDataDictionary[contentId].Data;
}
Expand Down Expand Up @@ -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<string, JToken>(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<string> GetSerializedFullContentsAsync(IEnumerable<string> 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.");
}
Expand All @@ -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;
}
Expand All @@ -177,7 +192,7 @@ public void ClearCache()

JsonContentsValidator.Validate(partiallyDeserialized);

this.cacheService.Set(cacheKay, contentsJson);
this.cacheService.Set(cacheKey, contentsJson);

return contentsJson;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Joystick.UnitTests/Utils/CacheHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}