peformance verbesserungen

This commit is contained in:
MarcWieland
2026-06-08 16:39:28 +02:00
parent b3e578308f
commit 708aa3991a
6 changed files with 73 additions and 13 deletions
@@ -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
@@ -6,6 +6,7 @@ namespace timetracker.Client.Services;
public class ClientTimetrackerService : ITimetrackerService
{
private readonly HttpClient _http;
private AppSettings? _cachedSettings;
public ClientTimetrackerService(HttpClient http)
{
@@ -24,12 +25,19 @@ public class ClientTimetrackerService : ITimetrackerService
public async Task<AppSettings> GetSettingsAsync(int userId)
{
return await _http.GetFromJsonAsync<AppSettings>($"api/tracker/settings/{userId}") ?? new AppSettings { UserId = userId };
if (_cachedSettings != null && _cachedSettings.UserId == userId)
{
return _cachedSettings;
}
var settings = await _http.GetFromJsonAsync<AppSettings>($"api/tracker/settings/{userId}");
_cachedSettings = settings ?? new AppSettings { UserId = userId };
return _cachedSettings;
}
public async Task SaveSettingsAsync(AppSettings settings)
{
await _http.PostAsJsonAsync("api/tracker/settings", settings);
_cachedSettings = settings;
}
public async Task<List<VacationDay>> GetVacationDaysAsync(int userId, int year)