Skip to content

Commit 4955bc4

Browse files
author
Anna Kolesnyk
committed
fix: add cache to GetContentAsync
1 parent cf8f101 commit 4955bc4

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/Joystick.Client/JoystickClient.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Net.Http;
@@ -99,11 +100,12 @@ public JoystickClient(
99100
public async Task<Dictionary<string, TData>> GetContentsAsync<TData>(IEnumerable<string> contentIds, JoystickContentOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
100101
{
101102
var settings = new GetContentSettings(options, typeof(TData));
102-
var serializedContents = await this.GetSerializedFullContentsAsync(contentIds, settings, cancellationToken);
103+
var enumerable = contentIds as string[] ?? contentIds.ToArray();
104+
var serializedContents = await this.GetSerializedFullContentsAsync(enumerable, settings, cancellationToken);
103105

104106
var contentDataDictionary = this.contentSerializer.Deserialize<Dictionary<string, JoystickBaseContent<TData>>>(serializedContents);
105107
var result = new Dictionary<string, TData>();
106-
foreach (var contentId in contentIds)
108+
foreach (var contentId in enumerable)
107109
{
108110
result[contentId] = contentDataDictionary[contentId].Data;
109111
}
@@ -141,19 +143,32 @@ public void ClearCache()
141143
throw new JoystickArgumentException($"{nameof(contentId)} can't be empty.");
142144
}
143145

144-
var contentsJson = await this.httpService.GetJsonContentsAsync(new[] { contentId }, settings, cancellationToken);
146+
var contentIds = new[] { contentId };
147+
148+
var cacheKay = CacheHelper.GenerateCacheKey(this.config, settings.IsContentSerialized, contentIds);
149+
var isContainedInCache = this.cacheService.TryGet(cacheKay, out var contentsJson);
150+
151+
if (!isContainedInCache || settings.Refresh == true)
152+
{
153+
contentsJson = await this.httpService.GetJsonContentsAsync(contentIds, settings, cancellationToken);
154+
}
145155

146156
var partiallyDeserialized = new Dictionary<string, JToken>(StringComparer.OrdinalIgnoreCase);
147157
JsonConvert.PopulateObject(contentsJson, partiallyDeserialized);
148158

149-
JsonContentsValidator.Validate(partiallyDeserialized);
159+
if (!isContainedInCache || settings.Refresh == true)
160+
{
161+
JsonContentsValidator.Validate(partiallyDeserialized);
162+
this.cacheService.Set(cacheKay, contentsJson);
163+
}
164+
150165
return partiallyDeserialized[contentId].ToString();
151166
}
152167

153168
private async Task<string> GetSerializedFullContentsAsync(IEnumerable<string> contentIds, GetContentSettings settings, CancellationToken cancellationToken = default(CancellationToken))
154169
{
155170
var enumerable = contentIds?.ToList();
156-
if (contentIds == null || !enumerable.Any())
171+
if (enumerable == null || !enumerable.Any())
157172
{
158173
throw new JoystickArgumentException($"{nameof(contentIds)} can't be empty.");
159174
}

0 commit comments

Comments
 (0)