-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
NEST-370: Create Media Theme caching for templates
- Loading branch information
Showing
6 changed files
with
102 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
Lombiq.Hosting.MediaTheme.Bridge/Services/IMediaThemeCachingService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Lombiq.Hosting.MediaTheme.Bridge.Models; | ||
using System.Threading.Tasks; | ||
|
||
namespace Lombiq.Hosting.MediaTheme.Bridge.Services; | ||
|
||
/// <summary> | ||
/// Media Theme template cache store and invalidation functions. | ||
/// </summary> | ||
public interface IMediaThemeCachingService | ||
{ | ||
/// <summary> | ||
/// Returns <see cref="MediaTemplate"/> if cached, else tries to fetch it from the storage. If not available stores | ||
/// and returns <see langword="null"/>. | ||
/// </summary> | ||
/// <param name="shapeType">The searched shape type.</param> | ||
Task<MediaTemplate> GetMemoryCachedMediaTemplateAsync(string shapeType); | ||
|
||
/// <summary> | ||
/// Deletes all Media Theme templates from the cache. | ||
/// </summary> | ||
Task InvalidateCachedMediaThemeTemplatesAsync(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
Lombiq.Hosting.MediaTheme.Bridge/Services/MediaThemeCachingService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Lombiq.Hosting.MediaTheme.Bridge.Models; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using OrchardCore.Environment.Cache; | ||
using System.Threading.Tasks; | ||
|
||
namespace Lombiq.Hosting.MediaTheme.Bridge.Services; | ||
|
||
public class MediaThemeCachingService : IMediaThemeCachingService | ||
{ | ||
private const string MediaThemeMemoryCacheKeyPrefix = "Lombiq.Hosting.MediaTheme.Bridge"; | ||
|
||
private readonly IMemoryCache _memoryCache; | ||
private readonly IMediaThemeManager _mediaThemeManager; | ||
private readonly ISignal _signal; | ||
|
||
public MediaThemeCachingService( | ||
IMemoryCache memoryCache, | ||
IMediaThemeManager mediaThemeManager, | ||
ISignal signal) | ||
{ | ||
_memoryCache = memoryCache; | ||
_mediaThemeManager = mediaThemeManager; | ||
_signal = signal; | ||
} | ||
|
||
public async Task<MediaTemplate> GetMemoryCachedMediaTemplateAsync(string shapeType) | ||
{ | ||
var cacheKey = $"{MediaThemeMemoryCacheKeyPrefix}:{shapeType}"; | ||
if (_memoryCache.TryGetValue(cacheKey, out MediaTemplate cachedMediaTemplate)) | ||
{ | ||
return cachedMediaTemplate; | ||
} | ||
|
||
cachedMediaTemplate = await _mediaThemeManager.GetMediaTemplateByShapeTypeAsync(shapeType); | ||
_memoryCache.Set(cacheKey, cachedMediaTemplate, _signal.GetToken(MediaThemeMemoryCacheKeyPrefix)); | ||
|
||
return cachedMediaTemplate; | ||
} | ||
|
||
public Task InvalidateCachedMediaThemeTemplatesAsync() => _signal.SignalTokenAsync(MediaThemeMemoryCacheKeyPrefix); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters