Skip to content

Commit 8aa4c29

Browse files
authored
Merge pull request #2096 from petriceko/AsyncCache
Fixes #2095
2 parents 3dac449 + 8316eaf commit 8aa4c29

17 files changed

+341
-226
lines changed

core/Piranha/Cache/ICache.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,23 @@ public interface ICache
2020
/// </summary>
2121
/// <typeparam name="T">The model type</typeparam>
2222
/// <param name="key">The unique key</param>
23+
/// <param name="cancellationToken"></param>
2324
/// <returns>The cached model, null it wasn't found</returns>
24-
T Get<T>(string key);
25+
Task<T> GetAsync<T>(string key, CancellationToken cancellationToken = default);
2526

2627
/// <summary>
2728
/// Sets the given model in the cache.
2829
/// </summary>
2930
/// <typeparam name="T">The model type</typeparam>
3031
/// <param name="key">The unique key</param>
3132
/// <param name="value">The model</param>
32-
void Set<T>(string key, T value);
33+
/// <param name="cancellationToken"></param>
34+
Task SetAsync<T>(string key, T value, CancellationToken cancellationToken = default);
3335

3436
/// <summary>
3537
/// Removes the model with the specified key from cache.
3638
/// </summary>
3739
/// <param name="key">The unique key</param>
38-
void Remove(string key);
40+
/// <param name="cancellationToken"></param>
41+
Task RemoveAsync(string key, CancellationToken cancellationToken = default);
3942
}

core/Piranha/Cache/Internal/DistributedCache.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,26 @@ public DistributedCache(IDistributedCache cache)
3333
}
3434

3535
/// <inheritdoc />
36-
public T Get<T>(string key)
36+
public async Task<T> GetAsync<T>(string key, CancellationToken cancellationToken = default)
3737
{
38-
var json = _cache.GetString(key);
38+
var json = await _cache.GetStringAsync(key, cancellationToken).ConfigureAwait(false);
3939

4040
if (!string.IsNullOrEmpty(json))
4141
{
4242
return JsonConvert.DeserializeObject<T>(json, _jsonSettings);
4343
}
44-
return default(T);
44+
return default;
4545
}
4646

4747
/// <inheritdoc />
48-
public void Set<T>(string key, T value)
48+
public async Task SetAsync<T>(string key, T value, CancellationToken cancellationToken = default)
4949
{
50-
_cache.SetString(key, JsonConvert.SerializeObject(value, _jsonSettings));
50+
await _cache.SetStringAsync(key, JsonConvert.SerializeObject(value, _jsonSettings), cancellationToken).ConfigureAwait(false);
5151
}
5252

5353
/// <inheritdoc />
54-
public void Remove(string key)
54+
public async Task RemoveAsync(string key, CancellationToken cancellationToken = default)
5555
{
56-
_cache.Remove(key);
56+
await _cache.RemoveAsync(key, cancellationToken).ConfigureAwait(false);
5757
}
5858
}

core/Piranha/Cache/Internal/MemoryCache.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,26 @@ public MemoryCache(IMemoryCache cache)
2727
}
2828

2929
/// <inheritdoc />
30-
public T Get<T>(string key)
30+
public Task<T> GetAsync<T>(string key, CancellationToken cancellationToken)
3131
{
3232
if (_cache.TryGetValue<T>(key, out var obj))
3333
{
34-
return obj;
34+
return Task.FromResult(obj);
3535
}
36-
return default(T);
36+
return Task.FromResult(default(T));
3737
}
3838

3939
/// <inheritdoc />
40-
public void Set<T>(string key, T value)
40+
public Task SetAsync<T>(string key, T value, CancellationToken cancellationToken)
4141
{
4242
_cache.Set(key, value);
43+
return Task.CompletedTask;
4344
}
4445

4546
/// <inheritdoc />
46-
public void Remove(string key)
47+
public Task RemoveAsync(string key, CancellationToken cancellationToken)
4748
{
4849
_cache.Remove(key);
50+
return Task.CompletedTask;
4951
}
5052
}

core/Piranha/Services/Internal/AliasService.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ public async Task<IEnumerable<Alias>> GetAllAsync(Guid? siteId = null)
7474
/// <returns>The model, or null if it doesn't exist</returns>
7575
public async Task<Alias> GetByIdAsync(Guid id)
7676
{
77-
var model = _cache?.Get<Alias>(id.ToString());
77+
var model = _cache == null ? null : await _cache.GetAsync<Alias>(id.ToString()).ConfigureAwait(false);
7878

7979
if (model == null)
8080
{
8181
model = await _repo.GetById(id).ConfigureAwait(false);
8282

83-
OnLoad(model);
83+
await OnLoad(model).ConfigureAwait(false);
8484
}
8585
return model;
8686
}
@@ -182,7 +182,7 @@ public async Task SaveAsync(Alias model)
182182
App.Hooks.OnAfterSave(model);
183183

184184
// Remove from cache
185-
RemoveFromCache(model);
185+
await RemoveFromCache(model).ConfigureAwait(false);
186186
}
187187

188188
/// <summary>
@@ -211,36 +211,38 @@ public async Task DeleteAsync(Alias model)
211211
App.Hooks.OnAfterDelete(model);
212212

213213
// Remove from cache
214-
RemoveFromCache(model);
214+
await RemoveFromCache(model).ConfigureAwait(false);
215215
}
216216

217217
/// <summary>
218218
/// Processes the model on load.
219219
/// </summary>
220220
/// <param name="model">The model</param>
221-
private void OnLoad(Alias model)
221+
private Task OnLoad(Alias model)
222222
{
223223
if (model != null)
224224
{
225225
App.Hooks.OnLoad(model);
226226

227227
if (_cache != null)
228228
{
229-
_cache.Set(model.Id.ToString(), model);
229+
return _cache.SetAsync(model.Id.ToString(), model);
230230
}
231231
}
232+
233+
return Task.CompletedTask;
232234
}
233235

234236
/// <summary>
235237
/// Removes the given model from cache.
236238
/// </summary>
237239
/// <param name="model">The model</param>
238-
private void RemoveFromCache(Alias model)
240+
private async Task RemoveFromCache(Alias model)
239241
{
240242
if (_cache != null)
241243
{
242-
_cache.Remove(model.Id.ToString());
243-
_cache.Remove($"Piranha_AliasUrls_{model.SiteId}");
244+
await _cache.RemoveAsync(model.Id.ToString()).ConfigureAwait(false);
245+
await _cache.RemoveAsync($"Piranha_AliasUrls_{model.SiteId}").ConfigureAwait(false);
244246
}
245247
}
246248

@@ -251,7 +253,7 @@ private async Task<IEnumerable<AliasUrlCacheEntry>> GetAliasUrls(Guid siteId)
251253
{
252254
if (_cache != null)
253255
{
254-
var aliasUrls = _cache.Get<IEnumerable<AliasUrlCacheEntry>>($"Piranha_AliasUrls_{siteId}");
256+
var aliasUrls = await _cache.GetAsync<IEnumerable<AliasUrlCacheEntry>>($"Piranha_AliasUrls_{siteId}").ConfigureAwait(false);
255257

256258
if (aliasUrls == null)
257259
{
@@ -262,7 +264,7 @@ private async Task<IEnumerable<AliasUrlCacheEntry>> GetAliasUrls(Guid siteId)
262264
AliasUrl = x.AliasUrl
263265
}).ToList();
264266

265-
_cache.Set($"Piranha_AliasUrls_{siteId}", aliasUrls);
267+
await _cache.SetAsync($"Piranha_AliasUrls_{siteId}", aliasUrls).ConfigureAwait(false);
266268
}
267269
return aliasUrls;
268270
}

core/Piranha/Services/Internal/ContentGroupService.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ public async Task SaveAsync(ContentGroup model)
7979
App.Hooks.OnAfterSave(model);
8080

8181
// Clear cache
82-
_cache?.Remove(CacheKey);
82+
if (_cache != null)
83+
{
84+
await _cache.RemoveAsync(CacheKey).ConfigureAwait(false);
85+
}
8386
}
8487

8588
/// <summary>
@@ -108,22 +111,26 @@ public async Task DeleteAsync(ContentGroup model)
108111
App.Hooks.OnAfterDelete(model);
109112

110113
// Clear cache
111-
_cache?.Remove(CacheKey);
114+
if (_cache != null)
115+
{
116+
await _cache.RemoveAsync(CacheKey).ConfigureAwait(false);
117+
}
112118
}
113119

114120
/// <summary>
115121
/// Gets the content types from the database.
116122
/// </summary>
117123
private async Task<IEnumerable<ContentGroup>> GetGroups()
118124
{
119-
if (_cache != null) {
120-
var groups = _cache.Get<IEnumerable<ContentGroup>>(CacheKey);
125+
if (_cache != null)
126+
{
127+
var groups = await _cache.GetAsync<IEnumerable<ContentGroup>>(CacheKey).ConfigureAwait(false);
121128

122129
if (groups == null)
123130
{
124131
groups = await _repo.GetAllAsync().ConfigureAwait(false);
125132

126-
_cache.Set(CacheKey, groups);
133+
await _cache.SetAsync(CacheKey, groups).ConfigureAwait(false);
127134
}
128135
return groups;
129136
}

core/Piranha/Services/Internal/ContentService.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,17 @@ public async Task<T> GetByIdAsync<T>(Guid id, Guid? languageId = null) where T :
135135
// First, try to get the model from cache
136136
if (typeof(T) == typeof(ContentInfo))
137137
{
138-
model = _cache?.Get<GenericContent>($"ContentInfo_{ languageId }_{ id }");
138+
if (_cache != null)
139+
{
140+
model = await _cache.GetAsync<GenericContent>($"ContentInfo_{languageId}_{id}").ConfigureAwait(false);
141+
}
139142
}
140143
else if (!typeof(DynamicContent).IsAssignableFrom(typeof(T)))
141144
{
142-
model = _cache?.Get<GenericContent>($"Content_{ languageId }_{ id }");
145+
if (_cache != null)
146+
{
147+
model = await _cache.GetAsync<GenericContent>($"Content_{languageId}_{id}").ConfigureAwait(false);
148+
}
143149
}
144150

145151
// If we have a model, let's initialize it
@@ -327,7 +333,7 @@ private async Task OnLoadAsync(GenericContent model, Guid languageId)
327333
}
328334

329335
// Initialize primary image
330-
if (model.PrimaryImage == null)
336+
if (model.PrimaryImage == null)
331337
{
332338
model.PrimaryImage = new Extend.Fields.ImageField();
333339
}
@@ -346,11 +352,11 @@ private async Task OnLoadAsync(GenericContent model, Guid languageId)
346352
// Store the model
347353
if (model is ContentInfo)
348354
{
349-
_cache.Set($"ContentInfo_{ languageId }_{ model.Id }", model);
355+
await _cache.SetAsync($"ContentInfo_{languageId}_{model.Id}", model).ConfigureAwait(false);
350356
}
351357
else if (model is not IDynamicContent)
352358
{
353-
_cache.Set($"Content_{ languageId }_{ model.Id }", model);
359+
await _cache.SetAsync($"Content_{languageId}_{model.Id}", model).ConfigureAwait(false);
354360
}
355361
}
356362
}
@@ -360,15 +366,12 @@ private async Task OnLoadAsync(GenericContent model, Guid languageId)
360366
/// </summary>
361367
/// <param name="model">The model</param>
362368
/// <param name="languageId">The language of the current model</param>
363-
private Task RemoveFromCacheAsync(GenericContent model, Guid languageId)
369+
private async Task RemoveFromCacheAsync(GenericContent model, Guid languageId)
364370
{
365-
return Task.Run(() =>
371+
if (_cache != null)
366372
{
367-
if (_cache != null)
368-
{
369-
_cache.Remove($"ContentInfo_{ languageId }_{ model.Id }");
370-
_cache.Remove($"Content_{ languageId }_{ model.Id }");
371-
}
372-
});
373+
await _cache.RemoveAsync($"ContentInfo_{languageId}_{model.Id}").ConfigureAwait(false);
374+
await _cache.RemoveAsync($"Content_{languageId}_{model.Id}").ConfigureAwait(false);
375+
}
373376
}
374377
}

core/Piranha/Services/Internal/ContentTypeService.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ public async Task SaveAsync(ContentType model)
109109
App.Hooks.OnAfterSave(model);
110110

111111
// Clear cache
112-
_cache?.Remove(CacheKey);
112+
if (_cache != null)
113+
{
114+
await _cache.RemoveAsync(CacheKey).ConfigureAwait(false);
115+
}
113116
}
114117

115118
/// <summary>
@@ -138,7 +141,10 @@ public async Task DeleteAsync(ContentType model)
138141
App.Hooks.OnAfterDelete(model);
139142

140143
// Clear cache
141-
_cache?.Remove(CacheKey);
144+
if (_cache != null)
145+
{
146+
await _cache.RemoveAsync(CacheKey).ConfigureAwait(false);
147+
}
142148
}
143149

144150
/// <summary>
@@ -157,7 +163,10 @@ public async Task DeleteAsync(IEnumerable<ContentType> models)
157163
App.Hooks.OnAfterDelete(model);
158164
}
159165
// Clear cache
160-
_cache?.Remove(CacheKey);
166+
if (_cache != null)
167+
{
168+
await _cache.RemoveAsync(CacheKey).ConfigureAwait(false);
169+
}
161170
}
162171
}
163172

@@ -168,13 +177,13 @@ private async Task<IEnumerable<ContentType>> GetTypes()
168177
{
169178
if (_cache != null)
170179
{
171-
var types = _cache.Get<IEnumerable<ContentType>>(CacheKey);
180+
var types = await _cache.GetAsync<IEnumerable<ContentType>>(CacheKey).ConfigureAwait(false);
172181

173182
if (types == null)
174183
{
175184
types = await _repo.GetAll().ConfigureAwait(false);
176185

177-
_cache.Set(CacheKey, types);
186+
await _cache.SetAsync(CacheKey, types).ConfigureAwait(false);
178187
}
179188
return types;
180189
}

core/Piranha/Services/Internal/LanguageService.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ public async Task SaveAsync(Language model)
120120
App.Hooks.OnAfterSave(model);
121121

122122
// Clear cache
123-
_cache?.Remove(CacheKey);
123+
if (_cache != null)
124+
{
125+
await _cache.RemoveAsync(CacheKey).ConfigureAwait(false);
126+
}
124127
}
125128

126129
/// <summary>
@@ -149,7 +152,10 @@ public async Task DeleteAsync(Language model)
149152
App.Hooks.OnAfterDelete(model);
150153

151154
// Clear cache
152-
_cache?.Remove(CacheKey);
155+
if (_cache != null)
156+
{
157+
await _cache.RemoveAsync(CacheKey).ConfigureAwait(false);
158+
}
153159
}
154160

155161
/// <summary>
@@ -159,13 +165,13 @@ private async Task<IEnumerable<Language>> GetLanguages()
159165
{
160166
if (_cache != null)
161167
{
162-
var types = _cache.Get<IEnumerable<Language>>(CacheKey);
168+
var types = await _cache.GetAsync<IEnumerable<Language>>(CacheKey).ConfigureAwait(false);
163169

164170
if (types == null)
165171
{
166172
types = await _repo.GetAll().ConfigureAwait(false);
167173

168-
_cache.Set(CacheKey, types);
174+
await _cache.SetAsync(CacheKey, types).ConfigureAwait(false);
169175
}
170176
return types;
171177
}

0 commit comments

Comments
 (0)