peformance verbesserungen
This commit is contained in:
@@ -6,6 +6,7 @@ namespace timetracker.Client.Services;
|
||||
public class ClientHolidayService : IHolidayService
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
private readonly Dictionary<(int Year, string StateCode), List<PublicHoliday>> _holidayCache = new();
|
||||
|
||||
public ClientHolidayService(HttpClient http)
|
||||
{
|
||||
@@ -14,12 +15,21 @@ public class ClientHolidayService : IHolidayService
|
||||
|
||||
public async Task<List<PublicHoliday>> GetHolidaysAsync(int year, string? stateCode = null)
|
||||
{
|
||||
var sc = stateCode ?? "";
|
||||
var key = (year, sc);
|
||||
if (_holidayCache.TryGetValue(key, out var cached))
|
||||
{
|
||||
return cached;
|
||||
}
|
||||
|
||||
var url = $"api/holidays?year={year}";
|
||||
if (!string.IsNullOrEmpty(stateCode))
|
||||
{
|
||||
url += $"&stateCode={stateCode}";
|
||||
}
|
||||
return await _http.GetFromJsonAsync<List<PublicHoliday>>(url) ?? [];
|
||||
var result = await _http.GetFromJsonAsync<List<PublicHoliday>>(url) ?? [];
|
||||
_holidayCache[key] = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<(bool Success, string Message)> FetchAndStoreAsync(int year)
|
||||
@@ -30,6 +40,12 @@ public class ClientHolidayService : IHolidayService
|
||||
var result = await response.Content.ReadFromJsonAsync<FetchResponse>();
|
||||
if (result != null)
|
||||
{
|
||||
// Invalidate the cache for this year
|
||||
var keysToRemove = _holidayCache.Keys.Where(k => k.Year == year).ToList();
|
||||
foreach (var k in keysToRemove)
|
||||
{
|
||||
_holidayCache.Remove(k);
|
||||
}
|
||||
return (result.Success, result.Message);
|
||||
}
|
||||
}
|
||||
@@ -39,6 +55,8 @@ public class ClientHolidayService : IHolidayService
|
||||
public async Task DeleteAsync(int id)
|
||||
{
|
||||
await _http.DeleteAsync($"api/holidays/{id}");
|
||||
// Invalidate all caches since we deleted a holiday
|
||||
_holidayCache.Clear();
|
||||
}
|
||||
|
||||
private class FetchResponse
|
||||
|
||||
Reference in New Issue
Block a user