From 7cc88c13f360351334fcf140940fdee2fef7856b Mon Sep 17 00:00:00 2001 From: OlgunR Date: Mon, 20 Apr 2026 11:48:50 +0200 Subject: [PATCH] Cache layout user to reduce localStorage access Add a private _cachedLayoutUser field to BandLayoutService and update EnsureLayoutUserAsync to cache the layout user value after first retrieval or generation. This avoids repeated localStorage calls and improves performance. --- DbFirst.BlazorWebApp/Services/BandLayoutService.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/DbFirst.BlazorWebApp/Services/BandLayoutService.cs b/DbFirst.BlazorWebApp/Services/BandLayoutService.cs index 4e1ae96..0082f80 100644 --- a/DbFirst.BlazorWebApp/Services/BandLayoutService.cs +++ b/DbFirst.BlazorWebApp/Services/BandLayoutService.cs @@ -9,16 +9,22 @@ namespace DbFirst.BlazorWebApp.Services { private const string LayoutUserStorageKey = "layoutUser"; private readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web); + private string? _cachedLayoutUser; public async Task EnsureLayoutUserAsync() { + if (!string.IsNullOrWhiteSpace(_cachedLayoutUser)) + return _cachedLayoutUser; + var layoutUser = await jsRuntime.InvokeAsync("localStorage.getItem", LayoutUserStorageKey); if (string.IsNullOrWhiteSpace(layoutUser)) { layoutUser = Guid.NewGuid().ToString("N"); await jsRuntime.InvokeVoidAsync("localStorage.setItem", LayoutUserStorageKey, layoutUser); } - return layoutUser; + + _cachedLayoutUser = layoutUser; + return _cachedLayoutUser; } public async Task LoadBandLayoutAsync(