-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(237738): improve redis caching edge cases (#920)
* fix: Perform redis caching at repository level and fix dependencies * chore: Linted code for plan-technology-for-your-school.sln solution * fix: fix not found pages being in cache forever * fix: move new deps into background job * chore: Linted code for plan-technology-for-your-school.sln solution * fix: fix cache test * fix: remove duplicate dep storage * chore: Linted code for plan-technology-for-your-school.sln solution * tests: add tests for improved redis cache invalidation * feat: clear redis cache on deployment * fix: format json redis key for better viewing * chore: separate content repository from cached content repository * chore: simplify redis format * fix: cache fetch by id and add tests * chore: change order of key * chore: Linted code for plan-technology-for-your-school.sln solution * tests: fix tests for reordered key * fix: content type should start with lower case * tests: update tests for casing --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
9c3840c
commit 891b5fc
Showing
29 changed files
with
381 additions
and
151 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
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
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
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
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
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
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
63 changes: 63 additions & 0 deletions
63
src/Dfe.PlanTech.Infrastructure.Contentful/Persistence/CachedContentfulRepository.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,63 @@ | ||
using Dfe.PlanTech.Application.Caching.Interfaces; | ||
using Dfe.PlanTech.Application.Persistence.Interfaces; | ||
using Dfe.PlanTech.Application.Persistence.Models; | ||
using Dfe.PlanTech.Domain.Persistence.Interfaces; | ||
using Dfe.PlanTech.Infrastructure.Contentful.Helpers; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Dfe.PlanTech.Infrastructure.Contentful.Persistence; | ||
|
||
/// <summary> | ||
/// Encapsulates ContentfulClient functionality, whilst abstracting through the IEntityRepository interface | ||
/// </summary> | ||
/// <see href="IEntityRepository"/> | ||
public class CachedContentfulRepository : IContentRepository | ||
{ | ||
private readonly IContentRepository _contentRepository; | ||
private readonly ICmsCache _cache; | ||
|
||
public CachedContentfulRepository([FromKeyedServices("contentfulRepository")] IContentRepository contentRepository, ICmsCache cache) | ||
{ | ||
_contentRepository = contentRepository ?? throw new ArgumentNullException(nameof(contentRepository)); | ||
_cache = cache ?? throw new ArgumentNullException(nameof(cache)); | ||
} | ||
|
||
public async Task<IEnumerable<TEntity>> GetEntities<TEntity>(CancellationToken cancellationToken = default) | ||
{ | ||
string contentType = GetContentTypeName<TEntity>(); | ||
var key = $"{contentType}s"; | ||
|
||
return await _cache.GetOrCreateAsync(key, async () => await _contentRepository.GetEntities<TEntity>(cancellationToken)) ?? []; | ||
} | ||
|
||
public async Task<IEnumerable<TEntity>> GetEntities<TEntity>(IGetEntitiesOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
var contentType = GetContentTypeName<TEntity>(); | ||
var jsonOptions = options.SerializeToRedisFormat(); | ||
var key = $"{contentType}{jsonOptions}"; | ||
|
||
return await _cache.GetOrCreateAsync(key, async () => await _contentRepository.GetEntities<TEntity>(options, cancellationToken)) ?? []; | ||
} | ||
|
||
public async Task<TEntity?> GetEntityById<TEntity>(string id, int include = 2, CancellationToken cancellationToken = default) | ||
{ | ||
var options = GetEntityByIdOptions(id, include); | ||
var entities = (await GetEntities<TEntity>(options, cancellationToken)).ToList(); | ||
|
||
if (entities.Count > 1) | ||
throw new GetEntitiesException($"Found more than 1 entity with id {id}"); | ||
|
||
return entities.FirstOrDefault(); | ||
} | ||
|
||
public GetEntitiesOptions GetEntityByIdOptions(string id, int include = 2) | ||
{ | ||
return _contentRepository.GetEntityByIdOptions(id, include); | ||
} | ||
|
||
private static string GetContentTypeName<TEntity>() | ||
{ | ||
var name = typeof(TEntity).Name; | ||
return name == "ContentSupportPage" ? name : name.FirstCharToLower(); | ||
} | ||
} |
Oops, something went wrong.