Skip to content

Commit

Permalink
Optimized services for usage without cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
tidyui committed Dec 5, 2023
1 parent e57719b commit cc3b484
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 87 deletions.
31 changes: 16 additions & 15 deletions core/Piranha/Services/Internal/ContentGroupService.cs
Expand Up @@ -40,9 +40,10 @@ public ContentGroupService(IContentGroupRepository repo, ICache cache = null)
/// Gets all available models.
/// </summary>
/// <returns>The available models</returns>
public Task<IEnumerable<ContentGroup>> GetAllAsync()
public async Task<IEnumerable<ContentGroup>> GetAllAsync()
{
return GetGroups();
var groups = await GetGroups().ConfigureAwait(false);
return groups ?? await _repo.GetAllAsync().ConfigureAwait(false);
}

/// <summary>
Expand All @@ -52,16 +53,13 @@ public Task<IEnumerable<ContentGroup>> GetAllAsync()
/// <returns>The model</returns>
public async Task<ContentGroup> 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);
}

/// <summary>
Expand Down Expand Up @@ -118,14 +116,17 @@ public async Task DeleteAsync(ContentGroup model)
/// </summary>
private async Task<IEnumerable<ContentGroup>> GetGroups()
{
var groups = _cache?.Get<IEnumerable<ContentGroup>>(CacheKey);
if (_cache != null) {
var groups = _cache.Get<IEnumerable<ContentGroup>>(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;
}
}
44 changes: 21 additions & 23 deletions core/Piranha/Services/Internal/ContentTypeService.cs
Expand Up @@ -40,9 +40,10 @@ public ContentTypeService(IContentTypeRepository repo, ICache cache = null)
/// Gets all available models.
/// </summary>
/// <returns>The available models</returns>
public Task<IEnumerable<ContentType>> GetAllAsync()
public async Task<IEnumerable<ContentType>> GetAllAsync()
{
return GetTypes();
var types = await GetTypes().ConfigureAwait(false);
return types ?? await _repo.GetAll().ConfigureAwait(false);
}

/// <summary>
Expand All @@ -52,17 +53,13 @@ public Task<IEnumerable<ContentType>> GetAllAsync()
/// <returns>The available models</returns>
public async Task<IEnumerable<ContentType>> 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);
}

/// <summary>
Expand All @@ -72,16 +69,13 @@ public async Task<IEnumerable<ContentType>> GetByGroupAsync(string group)
/// <returns>The model</returns>
public async Task<ContentType> 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);
}

/// <summary>
Expand Down Expand Up @@ -172,14 +166,18 @@ public async Task DeleteAsync(IEnumerable<ContentType> models)
/// </summary>
private async Task<IEnumerable<ContentType>> GetTypes()
{
var types = _cache?.Get<IEnumerable<ContentType>>(CacheKey);

if (types == null)
if (_cache != null)
{
types = await _repo.GetAll().ConfigureAwait(false);
var types = _cache.Get<IEnumerable<ContentType>>(CacheKey);

_cache?.Set(CacheKey, types);
if (types == null)
{
types = await _repo.GetAll().ConfigureAwait(false);

_cache.Set(CacheKey, types);
}
return types;
}
return types;
return null;
}
}
43 changes: 21 additions & 22 deletions core/Piranha/Services/Internal/LanguageService.cs
Expand Up @@ -40,9 +40,10 @@ public LanguageService(ILanguageRepository repo, ICache cache = null)
/// Gets all available models.
/// </summary>
/// <returns>The available models</returns>
public Task<IEnumerable<Language>> GetAllAsync()
public async Task<IEnumerable<Language>> GetAllAsync()
{
return GetLanguages();
var languages = await GetLanguages().ConfigureAwait(false);
return languages ?? await _repo.GetAll().ConfigureAwait(false);
}

/// <summary>
Expand All @@ -52,16 +53,13 @@ public Task<IEnumerable<Language>> GetAllAsync()
/// <returns>The model</returns>
public async Task<Language> 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);
}

/// <summary>
Expand All @@ -70,16 +68,13 @@ public async Task<Language> GetByIdAsync(Guid id)
/// <returns>The modell</returns>
public async Task<Language> 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);
}

/// <summary>
Expand Down Expand Up @@ -162,14 +157,18 @@ public async Task DeleteAsync(Language model)
/// </summary>
private async Task<IEnumerable<Language>> GetLanguages()
{
var types = _cache?.Get<IEnumerable<Language>>(CacheKey);

if (types == null)
if (_cache != null)
{
types = await _repo.GetAll().ConfigureAwait(false);
var types = _cache.Get<IEnumerable<Language>>(CacheKey);

_cache?.Set(CacheKey, types);
if (types == null)
{
types = await _repo.GetAll().ConfigureAwait(false);

_cache.Set(CacheKey, types);
}
return types;
}
return types;
return null;
}
}
27 changes: 18 additions & 9 deletions core/Piranha/Services/Internal/PageTypeService.cs
Expand Up @@ -39,9 +39,10 @@ public PageTypeService(IPageTypeRepository repo, ICache cache = null)
/// Gets all available models.
/// </summary>
/// <returns>The available models</returns>
public Task<IEnumerable<PageType>> GetAllAsync()
public async Task<IEnumerable<PageType>> GetAllAsync()
{
return GetTypes();
var types = await GetTypes().ConfigureAwait(false);
return types ?? await _repo.GetAll().ConfigureAwait(false);
}

/// <summary>
Expand All @@ -53,7 +54,11 @@ public async Task<PageType> 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);
}

/// <summary>
Expand Down Expand Up @@ -130,14 +135,18 @@ public async Task DeleteAsync(IEnumerable<PageType> models)
/// </summary>
private async Task<IEnumerable<PageType>> GetTypes()
{
var types = _cache?.Get<IEnumerable<PageType>>("Piranha_PageTypes");

if (types == null)
if (_cache != null)
{
types = await _repo.GetAll().ConfigureAwait(false);
var types = _cache.Get<IEnumerable<PageType>>("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;
}
}
27 changes: 18 additions & 9 deletions core/Piranha/Services/Internal/PostTypeService.cs
Expand Up @@ -39,9 +39,10 @@ public PostTypeService(IPostTypeRepository repo, ICache cache = null)
/// Gets all available models.
/// </summary>
/// <returns>The available models</returns>
public Task<IEnumerable<PostType>> GetAllAsync()
public async Task<IEnumerable<PostType>> GetAllAsync()
{
return GetTypes();
var types = await GetTypes().ConfigureAwait(false);
return types ?? await _repo.GetAll().ConfigureAwait(false);
}

/// <summary>
Expand All @@ -53,7 +54,11 @@ public async Task<PostType> 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);
}

/// <summary>
Expand Down Expand Up @@ -130,14 +135,18 @@ public async Task DeleteAsync(IEnumerable<PostType> models)
/// </summary>
private async Task<IEnumerable<PostType>> GetTypes()
{
var types = _cache?.Get<IEnumerable<PostType>>("Piranha_PostTypes");

if (types == null)
if (_cache != null)
{
types = await _repo.GetAll().ConfigureAwait(false);
var types = _cache.Get<IEnumerable<PostType>>("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;
}
}
27 changes: 18 additions & 9 deletions core/Piranha/Services/Internal/SiteTypeService.cs
Expand Up @@ -39,9 +39,10 @@ public SiteTypeService(ISiteTypeRepository repo, ICache cache = null)
/// Gets all available models.
/// </summary>
/// <returns>The available models</returns>
public Task<IEnumerable<SiteType>> GetAllAsync()
public async Task<IEnumerable<SiteType>> GetAllAsync()
{
return GetTypes();
var types = await GetTypes().ConfigureAwait(false);
return types ?? await _repo.GetAll().ConfigureAwait(false);
}

/// <summary>
Expand All @@ -53,7 +54,11 @@ public async Task<SiteType> 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);
}

/// <summary>
Expand Down Expand Up @@ -130,14 +135,18 @@ public async Task DeleteAsync(IEnumerable<SiteType> models)
/// </summary>
private async Task<IEnumerable<SiteType>> GetTypes()
{
var types = _cache?.Get<IEnumerable<SiteType>>("Piranha_SiteTypes");

if (types == null)
if (_cache != null)
{
types = await _repo.GetAll().ConfigureAwait(false);
var types = _cache.Get<IEnumerable<SiteType>>("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;
}
}

0 comments on commit cc3b484

Please sign in to comment.