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.
This commit is contained in:
@@ -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<string> EnsureLayoutUserAsync()
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(_cachedLayoutUser))
|
||||
return _cachedLayoutUser;
|
||||
|
||||
var layoutUser = await jsRuntime.InvokeAsync<string?>("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<BandLayout> LoadBandLayoutAsync(
|
||||
|
||||
Reference in New Issue
Block a user