|
1 | 1 | using System; |
| 2 | +using System.Collections; |
2 | 3 | using System.Collections.Generic; |
3 | 4 | using System.Linq; |
4 | 5 | using System.Net.Http; |
@@ -99,11 +100,12 @@ public JoystickClient( |
99 | 100 | public async Task<Dictionary<string, TData>> GetContentsAsync<TData>(IEnumerable<string> contentIds, JoystickContentOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) |
100 | 101 | { |
101 | 102 | 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); |
103 | 105 |
|
104 | 106 | var contentDataDictionary = this.contentSerializer.Deserialize<Dictionary<string, JoystickBaseContent<TData>>>(serializedContents); |
105 | 107 | var result = new Dictionary<string, TData>(); |
106 | | - foreach (var contentId in contentIds) |
| 108 | + foreach (var contentId in enumerable) |
107 | 109 | { |
108 | 110 | result[contentId] = contentDataDictionary[contentId].Data; |
109 | 111 | } |
@@ -141,19 +143,32 @@ public void ClearCache() |
141 | 143 | throw new JoystickArgumentException($"{nameof(contentId)} can't be empty."); |
142 | 144 | } |
143 | 145 |
|
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 | + } |
145 | 155 |
|
146 | 156 | var partiallyDeserialized = new Dictionary<string, JToken>(StringComparer.OrdinalIgnoreCase); |
147 | 157 | JsonConvert.PopulateObject(contentsJson, partiallyDeserialized); |
148 | 158 |
|
149 | | - JsonContentsValidator.Validate(partiallyDeserialized); |
| 159 | + if (!isContainedInCache || settings.Refresh == true) |
| 160 | + { |
| 161 | + JsonContentsValidator.Validate(partiallyDeserialized); |
| 162 | + this.cacheService.Set(cacheKay, contentsJson); |
| 163 | + } |
| 164 | + |
150 | 165 | return partiallyDeserialized[contentId].ToString(); |
151 | 166 | } |
152 | 167 |
|
153 | 168 | private async Task<string> GetSerializedFullContentsAsync(IEnumerable<string> contentIds, GetContentSettings settings, CancellationToken cancellationToken = default(CancellationToken)) |
154 | 169 | { |
155 | 170 | var enumerable = contentIds?.ToList(); |
156 | | - if (contentIds == null || !enumerable.Any()) |
| 171 | + if (enumerable == null || !enumerable.Any()) |
157 | 172 | { |
158 | 173 | throw new JoystickArgumentException($"{nameof(contentIds)} can't be empty."); |
159 | 174 | } |
|
0 commit comments