From cc3b4848c165de296eeb06b55e1d37bd22ba6959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kan=20Edling?= Date: Tue, 5 Dec 2023 21:58:35 +0100 Subject: [PATCH] Optimized services for usage without cache. --- .../Services/Internal/ContentGroupService.cs | 31 ++++++------- .../Services/Internal/ContentTypeService.cs | 44 +++++++++---------- .../Services/Internal/LanguageService.cs | 43 +++++++++--------- .../Services/Internal/PageTypeService.cs | 27 ++++++++---- .../Services/Internal/PostTypeService.cs | 27 ++++++++---- .../Services/Internal/SiteTypeService.cs | 27 ++++++++---- 6 files changed, 112 insertions(+), 87 deletions(-) diff --git a/core/Piranha/Services/Internal/ContentGroupService.cs b/core/Piranha/Services/Internal/ContentGroupService.cs index e2260b1c5..b48d6c728 100644 --- a/core/Piranha/Services/Internal/ContentGroupService.cs +++ b/core/Piranha/Services/Internal/ContentGroupService.cs @@ -40,9 +40,10 @@ public ContentGroupService(IContentGroupRepository repo, ICache cache = null) /// Gets all available models. /// /// The available models - public Task> GetAllAsync() + public async Task> GetAllAsync() { - return GetGroups(); + var groups = await GetGroups().ConfigureAwait(false); + return groups ?? await _repo.GetAllAsync().ConfigureAwait(false); } /// @@ -52,16 +53,13 @@ public Task> GetAllAsync() /// The model public async Task GetByIdAsync(string id) { - if (_cache != null && App.CacheLevel != CacheLevel.None) - { - var groups = await GetGroups().ConfigureAwait(false); + var groups = await GetGroups().ConfigureAwait(false); - return groups.FirstOrDefault(t => t.Id == id); - } - else + if (groups != null) { - return await _repo.GetByIdAsync(id).ConfigureAwait(false); + return groups.FirstOrDefault(t => t.Id == id); } + return await _repo.GetByIdAsync(id).ConfigureAwait(false); } /// @@ -118,14 +116,17 @@ public async Task DeleteAsync(ContentGroup model) /// private async Task> GetGroups() { - var groups = _cache?.Get>(CacheKey); + if (_cache != null) { + var groups = _cache.Get>(CacheKey); - if (groups == null) - { - groups = await _repo.GetAllAsync().ConfigureAwait(false); + if (groups == null) + { + groups = await _repo.GetAllAsync().ConfigureAwait(false); - _cache?.Set(CacheKey, groups); + _cache.Set(CacheKey, groups); + } + return groups; } - return groups; + return null; } } diff --git a/core/Piranha/Services/Internal/ContentTypeService.cs b/core/Piranha/Services/Internal/ContentTypeService.cs index 1549b865d..f8335f637 100644 --- a/core/Piranha/Services/Internal/ContentTypeService.cs +++ b/core/Piranha/Services/Internal/ContentTypeService.cs @@ -40,9 +40,10 @@ public ContentTypeService(IContentTypeRepository repo, ICache cache = null) /// Gets all available models. /// /// The available models - public Task> GetAllAsync() + public async Task> GetAllAsync() { - return GetTypes(); + var types = await GetTypes().ConfigureAwait(false); + return types ?? await _repo.GetAll().ConfigureAwait(false); } /// @@ -52,17 +53,13 @@ public Task> GetAllAsync() /// The available models public async Task> GetByGroupAsync(string group) { - // Check if we have cache enabled - if (_cache != null && App.CacheLevel != CacheLevel.None) - { - var types = await GetTypes().ConfigureAwait(false); + var types = await GetTypes().ConfigureAwait(false); - return types.Where(t => t.Group == group).ToList(); - } - else + if (types != null) { - return await _repo.GetByGroup(group).ConfigureAwait(false); + return types.Where(t => t.Group == group).ToList(); } + return await _repo.GetByGroup(group).ConfigureAwait(false); } /// @@ -72,16 +69,13 @@ public async Task> GetByGroupAsync(string group) /// The model public async Task GetByIdAsync(string id) { - if (_cache != null && App.CacheLevel != CacheLevel.None) - { - var types = await GetTypes().ConfigureAwait(false); + var types = await GetTypes().ConfigureAwait(false); - return types.FirstOrDefault(t => t.Id == id); - } - else + if (types != null) { - return await _repo.GetById(id).ConfigureAwait(false); + return types.FirstOrDefault(t => t.Id == id); } + return await _repo.GetById(id).ConfigureAwait(false); } /// @@ -172,14 +166,18 @@ public async Task DeleteAsync(IEnumerable models) /// private async Task> GetTypes() { - var types = _cache?.Get>(CacheKey); - - if (types == null) + if (_cache != null) { - types = await _repo.GetAll().ConfigureAwait(false); + var types = _cache.Get>(CacheKey); - _cache?.Set(CacheKey, types); + if (types == null) + { + types = await _repo.GetAll().ConfigureAwait(false); + + _cache.Set(CacheKey, types); + } + return types; } - return types; + return null; } } diff --git a/core/Piranha/Services/Internal/LanguageService.cs b/core/Piranha/Services/Internal/LanguageService.cs index cf6664747..8083dd032 100644 --- a/core/Piranha/Services/Internal/LanguageService.cs +++ b/core/Piranha/Services/Internal/LanguageService.cs @@ -40,9 +40,10 @@ public LanguageService(ILanguageRepository repo, ICache cache = null) /// Gets all available models. /// /// The available models - public Task> GetAllAsync() + public async Task> GetAllAsync() { - return GetLanguages(); + var languages = await GetLanguages().ConfigureAwait(false); + return languages ?? await _repo.GetAll().ConfigureAwait(false); } /// @@ -52,16 +53,13 @@ public Task> GetAllAsync() /// The model public async Task GetByIdAsync(Guid id) { - if (_cache != null && App.CacheLevel != CacheLevel.None) - { - var languages = await GetLanguages().ConfigureAwait(false); + var languages = await GetLanguages().ConfigureAwait(false); - return languages.FirstOrDefault(l => l.Id == id); - } - else + if (languages != null) { - return await _repo.GetById(id).ConfigureAwait(false); + return languages.FirstOrDefault(t => t.Id == id); } + return await _repo.GetById(id).ConfigureAwait(false); } /// @@ -70,16 +68,13 @@ public async Task GetByIdAsync(Guid id) /// The modell public async Task GetDefaultAsync() { - if (_cache != null && App.CacheLevel != CacheLevel.None) - { - var languages = await GetLanguages().ConfigureAwait(false); + var languages = await GetLanguages().ConfigureAwait(false); - return languages.FirstOrDefault(l => l.IsDefault); - } - else + if (languages != null) { - return await _repo.GetDefault().ConfigureAwait(false); + return languages.FirstOrDefault(l => l.IsDefault); } + return await _repo.GetDefault().ConfigureAwait(false); } /// @@ -162,14 +157,18 @@ public async Task DeleteAsync(Language model) /// private async Task> GetLanguages() { - var types = _cache?.Get>(CacheKey); - - if (types == null) + if (_cache != null) { - types = await _repo.GetAll().ConfigureAwait(false); + var types = _cache.Get>(CacheKey); - _cache?.Set(CacheKey, types); + if (types == null) + { + types = await _repo.GetAll().ConfigureAwait(false); + + _cache.Set(CacheKey, types); + } + return types; } - return types; + return null; } } diff --git a/core/Piranha/Services/Internal/PageTypeService.cs b/core/Piranha/Services/Internal/PageTypeService.cs index 9495fd5a8..5caeec1e3 100644 --- a/core/Piranha/Services/Internal/PageTypeService.cs +++ b/core/Piranha/Services/Internal/PageTypeService.cs @@ -39,9 +39,10 @@ public PageTypeService(IPageTypeRepository repo, ICache cache = null) /// Gets all available models. /// /// The available models - public Task> GetAllAsync() + public async Task> GetAllAsync() { - return GetTypes(); + var types = await GetTypes().ConfigureAwait(false); + return types ?? await _repo.GetAll().ConfigureAwait(false); } /// @@ -53,7 +54,11 @@ public async Task GetByIdAsync(string id) { var types = await GetTypes().ConfigureAwait(false); - return types.FirstOrDefault(t => t.Id == id); + if (types != null) + { + return types.FirstOrDefault(t => t.Id == id); + } + return await _repo.GetById(id).ConfigureAwait(false); } /// @@ -130,14 +135,18 @@ public async Task DeleteAsync(IEnumerable models) /// private async Task> GetTypes() { - var types = _cache?.Get>("Piranha_PageTypes"); - - if (types == null) + if (_cache != null) { - types = await _repo.GetAll().ConfigureAwait(false); + var types = _cache.Get>("Piranha_PageTypes"); - _cache?.Set("Piranha_PageTypes", types); + if (types == null) + { + types = await _repo.GetAll().ConfigureAwait(false); + + _cache.Set("Piranha_PageTypes", types); + } + return types; } - return types; + return null; } } diff --git a/core/Piranha/Services/Internal/PostTypeService.cs b/core/Piranha/Services/Internal/PostTypeService.cs index 65957ea29..794e62cd4 100644 --- a/core/Piranha/Services/Internal/PostTypeService.cs +++ b/core/Piranha/Services/Internal/PostTypeService.cs @@ -39,9 +39,10 @@ public PostTypeService(IPostTypeRepository repo, ICache cache = null) /// Gets all available models. /// /// The available models - public Task> GetAllAsync() + public async Task> GetAllAsync() { - return GetTypes(); + var types = await GetTypes().ConfigureAwait(false); + return types ?? await _repo.GetAll().ConfigureAwait(false); } /// @@ -53,7 +54,11 @@ public async Task GetByIdAsync(string id) { var types = await GetTypes().ConfigureAwait(false); - return types.FirstOrDefault(t => t.Id == id); + if (types != null) + { + return types.FirstOrDefault(t => t.Id == id); + } + return await _repo.GetById(id).ConfigureAwait(false); } /// @@ -130,14 +135,18 @@ public async Task DeleteAsync(IEnumerable models) /// private async Task> GetTypes() { - var types = _cache?.Get>("Piranha_PostTypes"); - - if (types == null) + if (_cache != null) { - types = await _repo.GetAll().ConfigureAwait(false); + var types = _cache.Get>("Piranha_PostTypes"); - _cache?.Set("Piranha_PostTypes", types); + if (types == null) + { + types = await _repo.GetAll().ConfigureAwait(false); + + _cache.Set("Piranha_PostTypes", types); + } + return types; } - return types; + return null; } } diff --git a/core/Piranha/Services/Internal/SiteTypeService.cs b/core/Piranha/Services/Internal/SiteTypeService.cs index 5021a4136..bb93e547d 100644 --- a/core/Piranha/Services/Internal/SiteTypeService.cs +++ b/core/Piranha/Services/Internal/SiteTypeService.cs @@ -39,9 +39,10 @@ public SiteTypeService(ISiteTypeRepository repo, ICache cache = null) /// Gets all available models. /// /// The available models - public Task> GetAllAsync() + public async Task> GetAllAsync() { - return GetTypes(); + var types = await GetTypes().ConfigureAwait(false); + return types ?? await _repo.GetAll().ConfigureAwait(false); } /// @@ -53,7 +54,11 @@ public async Task GetByIdAsync(string id) { var types = await GetTypes().ConfigureAwait(false); - return types.FirstOrDefault(t => t.Id == id); + if (types != null) + { + return types.FirstOrDefault(t => t.Id == id); + } + return await _repo.GetById(id).ConfigureAwait(false); } /// @@ -130,14 +135,18 @@ public async Task DeleteAsync(IEnumerable models) /// private async Task> GetTypes() { - var types = _cache?.Get>("Piranha_SiteTypes"); - - if (types == null) + if (_cache != null) { - types = await _repo.GetAll().ConfigureAwait(false); + var types = _cache.Get>("Piranha_SiteTypes"); - _cache?.Set("Piranha_SiteTypes", types); + if (types == null) + { + types = await _repo.GetAll().ConfigureAwait(false); + + _cache.Set("Piranha_SiteTypes", types); + } + return types; } - return types; + return null; } }