cachingblazorparameter-passingsession-store

Protected Local Storage is very slow


I have 13 parameters that when a user log in website will be set like this:

`ProtectedSessionStore.SetAsync("iduser", iduser);
ProtectedSessionStore.SetAsync("unitid", unitid);
ProtectedSessionStore.SetAsync("branchid", branchid);
ProtectedSessionStore.SetAsync("organid", organid);
ProtectedSessionStore.SetAsync("defuser", defuser);
ProtectedSessionStore.SetAsync("defbranch", defbranch);
ProtectedSessionStore.SetAsync("defunit", defunit);
ProtectedSessionStore.SetAsync("defproduct", defproduct);
ProtectedSessionStore.SetAsync("defpointofbody", defpointofbody);
ProtectedSessionStore.SetAsync("defdegree", defdegree);
ProtectedSessionStore.SetAsync("defvisit", defvisit);
ProtectedSessionStore.SetAsync("defpatient", defpatient);
ProtectedSessionStore.SetAsync("report", report);`

after login I want to have this values in 2 razor pages. One of the pages is my navmenu layout and the other one is mainmenu. so in both I have this code:

 var result = await ProtectedSessionStore.GetAsync<int>("iduser");
iduser = result.Success ? result.Value : 0;
result = await ProtectedSessionStore.GetAsync<int>("unitid");
unitid = result.Success ? result.Value : 0;
result = await ProtectedSessionStore.GetAsync<int>("branchid");
branchid = result.Success ? result.Value : 0;
result = await ProtectedSessionStore.GetAsync<int>("organid");
organid = result.Success ? result.Value : 0;
var result1 = await ProtectedSessionStore.GetAsync<bool>("defuser");
defuser = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defbranch");
defbranch = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defunit");
defunit = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defvisit");
defvisit = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defdegree");
defdegree = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defpointofbody");
defpointofbody = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defpatient");
defpatient = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("defproduct");
defproduct = result1.Success ? result1.Value : false;
result1 = await ProtectedSessionStore.GetAsync<bool>("report");
report = result1.Success ? result1.Value : false;

the problem is that it is very slow loading this parameters. How can I speed up this Process?


Solution

  • First of all, creating one object having all those fields is the best way to go.

    public class SessionData
    {
        public int IdUser { get; set; }
        public int UnitId { get; set; }
        public int BranchId { get; set; }
        public int OrganId { get; set; }
        public bool DefUser { get; set; }
        public bool DefBranch { get; set; }
        public bool DefUnit { get; set; }
        public bool DefVisit { get; set; }
        public bool DefDegree { get; set; }
        public bool DefPointOfBody { get; set; }
        public bool DefPatient { get; set; }
        public bool DefProduct { get; set; }
        public bool Report { get; set; }
    }
    

    Create a State Management Service, let's call it SessionStateService, this one will manage these relevant data across your web app during the session.

    Register it as Scoped, or even as Singleton if it's a Blazor WebAssmebly application.

    For example, your service can be something like this:

    public class SessionDataService
    {
        private readonly ProtectedSessionStore _protectedSessionStore;
        public SessionData SessionData { get; private set; }
    
        public SessionDataService(ProtectedSessionStore protectedSessionStore)
        {
            _protectedSessionStore = protectedSessionStore;
            SessionData = new SessionData(); // Initialize with default values
        }
    
        public async Task LoadSessionDataAsync()
        {
            var result = await _protectedSessionStore.GetAsync<string>("sessionData");
            if (result.Success && !string.IsNullOrEmpty(result.Value))
            {
                SessionData = JsonSerializer.Deserialize<SessionData>(result.Value);
            }
            else
            {
                SessionData = new SessionData(); // Initialize with default values if not found
            }
        }
    
        public async Task StoreSessionDataAsync()
        {
            var sessionDataJson = JsonSerializer.Serialize(SessionData);
            await _protectedSessionStore.SetAsync("sessionData", sessionDataJson);
        }
    
        public void SetSessionData(SessionData sessionData)
        {
            SessionData = sessionData;
        }
    }
    

    You can eventually access the stored data this way:

    @page "/example"
    @inject SessionDataService SessionDataService
    
    <h3>Example Page</h3>
    
    <p>IdUser: @SessionDataService.SessionData.IdUser</p>
    
    <button @onclick="UpdateSessionData">Update Session Data</button>
    
    @code {
        private void UpdateSessionData()
        {
            SessionDataService.SessionData.IdUser = 123;
            // Save changes back to ProtectedSessionStore
            SessionDataService.StoreSessionDataAsync();
        }
    }