-
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.
Co-authored-by: Ida Marie Andreassen <[email protected]>
- Loading branch information
1 parent
37b315c
commit a09fcd9
Showing
7 changed files
with
243 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Core.PlannedAbsences; | ||
|
||
public interface IPlannedAbsenceRepository | ||
{ | ||
public Task<Dictionary<int, List<PlannedAbsence>>> GetPlannedAbsenceForConsultants(List<int> consultantIds, | ||
CancellationToken ct); | ||
|
||
public Task<List<PlannedAbsence>> GetPlannedAbsenceForConsultant(int consultantId, CancellationToken ct); | ||
|
||
public Task UpsertPlannedAbsence(PlannedAbsence plannedAbsence, CancellationToken ct); | ||
|
||
public Task UpsertMultiplePlannedAbsences(List<PlannedAbsence> plannedAbsences, CancellationToken ct); | ||
} |
75 changes: 75 additions & 0 deletions
75
backend/Infrastructure/Repositories/PlannedAbsences/PlannedAbsenceCacheRepository.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,75 @@ | ||
using Core.PlannedAbsences; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace Infrastructure.Repositories.PlannedAbsences; | ||
|
||
public class PlannedAbsenceCacheRepository(IPlannedAbsenceRepository sourceRepository, IMemoryCache cache) : IPlannedAbsenceRepository | ||
{ | ||
public async Task<Dictionary<int, List<PlannedAbsence>>> GetPlannedAbsenceForConsultants(List<int> consultantIds, CancellationToken ct) | ||
{ | ||
var nonCachedIds = new List<int>(); | ||
var result = new Dictionary<int, List<PlannedAbsence>>(); | ||
|
||
foreach (var consultantId in consultantIds.Distinct()) | ||
{ | ||
var plannedAbsenceList = GetPlannedAbsencesFromCache(consultantId); | ||
if (plannedAbsenceList is null) | ||
nonCachedIds.Add(consultantId); | ||
else | ||
result.Add(consultantId, plannedAbsenceList); | ||
} | ||
|
||
var queriedPlannedAbsenceLists = await sourceRepository.GetPlannedAbsenceForConsultants(nonCachedIds, ct); | ||
foreach (var (cId, plannedAbsences) in queriedPlannedAbsenceLists) | ||
{ | ||
result.Add(cId, plannedAbsences); | ||
cache.Set(PlannedAbsenceCacheKey(cId), plannedAbsences); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public async Task<List<PlannedAbsence>> GetPlannedAbsenceForConsultant(int consultantId, CancellationToken ct) | ||
{ | ||
var plannedAbsenceList = GetPlannedAbsencesFromCache(consultantId); | ||
if (plannedAbsenceList is not null) return plannedAbsenceList; | ||
|
||
plannedAbsenceList = await sourceRepository.GetPlannedAbsenceForConsultant(consultantId, ct); | ||
cache.Set(PlannedAbsenceCacheKey(consultantId), plannedAbsenceList); | ||
return plannedAbsenceList; | ||
} | ||
|
||
public async Task UpsertPlannedAbsence(PlannedAbsence plannedAbsence, CancellationToken ct) | ||
{ | ||
await sourceRepository.UpsertPlannedAbsence(plannedAbsence, ct); | ||
ClearPlannedAbsenceCache(plannedAbsence.ConsultantId); | ||
} | ||
|
||
public async Task UpsertMultiplePlannedAbsences(List<PlannedAbsence> plannedAbsences, CancellationToken ct) | ||
{ | ||
await sourceRepository.UpsertMultiplePlannedAbsences(plannedAbsences, ct); | ||
|
||
var consultantIds = plannedAbsences.Select(pa => pa.ConsultantId).Distinct(); | ||
foreach (var consultantId in consultantIds) ClearPlannedAbsenceCache(consultantId); | ||
|
||
} | ||
|
||
private List<PlannedAbsence>? GetPlannedAbsencesFromCache(int consultantId) | ||
{ | ||
if (cache.TryGetValue<List<PlannedAbsence>>(PlannedAbsenceCacheKey(consultantId), out var plannedAbsenceList)) | ||
if (plannedAbsenceList is not null) | ||
return plannedAbsenceList; | ||
|
||
return null; | ||
} | ||
|
||
private void ClearPlannedAbsenceCache(int consultantId) | ||
{ | ||
cache.Remove(PlannedAbsenceCacheKey(consultantId)); | ||
} | ||
|
||
private static string PlannedAbsenceCacheKey(int consultantId) | ||
{ | ||
return $"PlannedAbsenceCacheRepository/{consultantId}"; | ||
} | ||
} |
Oops, something went wrong.