Skip to content

Commit ae3759c

Browse files
committed
Base64 is to slow
1 parent 38c29e0 commit ae3759c

1 file changed

Lines changed: 65 additions & 59 deletions

File tree

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using System.Buffers.Text;
2-
using System.Text;
31
using System.Text.Json;
2+
using Drogecode.Blazor.ExpireStorage.Helpers;
43
using Microsoft.JSInterop;
54

65
namespace Drogecode.Blazor.ExpireStorage;
@@ -17,87 +16,94 @@ public JsStorageService(IJSRuntime jsRuntime)
1716

1817
public T RetrieveItem<T>(string storageKey, StorageLocation storage, T defaultIfNull)
1918
{
20-
var stringFromCache = storage switch
19+
try
2120
{
22-
StorageLocation.BrowserLocal => ((IJSInProcessRuntime)_jsRuntime).Invoke<string?>("localStorage.getItem", storageKey) ?? string.Empty,
23-
StorageLocation.BrowserSession => ((IJSInProcessRuntime)_jsRuntime).Invoke<string?>("sessionStorage.getItem", storageKey) ?? string.Empty,
24-
_ => _pageCache[storageKey]
25-
};
26-
if (string.IsNullOrEmpty(stringFromCache)) return defaultIfNull;
21+
var stringFromCache = storage switch
22+
{
23+
StorageLocation.BrowserLocal => ((IJSInProcessRuntime)_jsRuntime).Invoke<string?>("localStorage.getItem", storageKey) ?? string.Empty,
24+
StorageLocation.BrowserSession => ((IJSInProcessRuntime)_jsRuntime).Invoke<string?>("sessionStorage.getItem", storageKey) ?? string.Empty,
25+
_ => _pageCache[storageKey]
26+
};
27+
if (string.IsNullOrEmpty(stringFromCache)) return defaultIfNull;
2728

28-
string jsonString;
29-
if (Base64.IsValid(stringFromCache))
30-
{
31-
var utf8Byes = Convert.FromBase64String(stringFromCache);
32-
jsonString = Encoding.UTF8.GetString(utf8Byes);
29+
return JsonSerializer.Deserialize<T>(stringFromCache) ?? defaultIfNull;
3330
}
34-
else
31+
catch (Exception ex)
3532
{
36-
jsonString = stringFromCache;
33+
ConsoleHelper.WriteLine("Exception in RetrieveItem", ex);
34+
return defaultIfNull;
3735
}
38-
39-
if (string.IsNullOrEmpty(jsonString)) return defaultIfNull;
40-
41-
return JsonSerializer.Deserialize<T>(jsonString) ?? defaultIfNull;
4236
}
4337

4438
public async Task<T?> RetrieveItem<T>(string storageKey, StorageLocation storageLocation, CancellationToken clt = default)
4539
{
46-
var stringFromCache = storageLocation switch
47-
{
48-
StorageLocation.BrowserLocal => await _jsRuntime.InvokeAsync<string?>("localStorage.getItem", clt, storageKey) ?? string.Empty,
49-
StorageLocation.BrowserSession => await _jsRuntime.InvokeAsync<string?>("sessionStorage.getItem", clt, storageKey) ?? string.Empty,
50-
_ => _pageCache.TryGetValue(storageKey, out string? cachedItem) ? cachedItem : string.Empty
51-
};
52-
string jsonString;
53-
if (Base64.IsValid(stringFromCache))
40+
try
5441
{
55-
var utf8Byes = Convert.FromBase64String(stringFromCache);
56-
jsonString = Encoding.UTF8.GetString(utf8Byes);
42+
var stringFromCache = storageLocation switch
43+
{
44+
StorageLocation.BrowserLocal => await _jsRuntime.InvokeAsync<string?>("localStorage.getItem", clt, storageKey) ?? string.Empty,
45+
StorageLocation.BrowserSession => await _jsRuntime.InvokeAsync<string?>("sessionStorage.getItem", clt, storageKey) ?? string.Empty,
46+
_ => _pageCache.TryGetValue(storageKey, out string? cachedItem) ? cachedItem : string.Empty
47+
};
48+
49+
if (string.IsNullOrEmpty(stringFromCache)) return default;
50+
51+
return JsonSerializer.Deserialize<T>(stringFromCache) ?? default;
5752
}
58-
else
53+
54+
catch (Exception ex)
5955
{
60-
jsonString = stringFromCache;
56+
ConsoleHelper.WriteLine("Exception in RetrieveItem (async)", ex);
57+
return default;
6158
}
62-
63-
if (string.IsNullOrEmpty(jsonString)) return default;
64-
65-
return JsonSerializer.Deserialize<T>(jsonString) ?? default;
6659
}
6760

6861
public async Task StoreItem<T>(string storageKey, StorageLocation storageLocation, T itemToStore, CancellationToken clt = default)
6962
{
70-
var utf8Byes = JsonSerializer.SerializeToUtf8Bytes<T>(itemToStore);
71-
var base64String = Convert.ToBase64String(utf8Byes);
63+
try
64+
{
65+
var asString = JsonSerializer.Serialize(itemToStore);
66+
67+
switch (storageLocation)
68+
{
69+
case StorageLocation.BrowserLocal:
70+
await _jsRuntime.InvokeVoidAsync("localStorage.setItem", clt, storageKey, asString);
71+
break;
72+
case StorageLocation.BrowserSession:
73+
await _jsRuntime.InvokeVoidAsync("sessionStorage.setItem", clt, storageKey, asString);
74+
break;
75+
default:
76+
_pageCache[storageKey] = asString;
77+
break;
78+
}
79+
}
7280

73-
switch (storageLocation)
81+
catch (Exception ex)
7482
{
75-
case StorageLocation.BrowserLocal:
76-
await _jsRuntime.InvokeVoidAsync("localStorage.setItem", clt, storageKey, base64String);
77-
break;
78-
case StorageLocation.BrowserSession:
79-
await _jsRuntime.InvokeVoidAsync("sessionStorage.setItem", clt, storageKey, base64String);
80-
break;
81-
default:
82-
_pageCache[storageKey] = base64String;
83-
break;
83+
ConsoleHelper.WriteLine("Exception in StoreItem", ex);
8484
}
8585
}
8686

87-
8887
public async Task RemoveItem(string storageKey, StorageLocation storageLocation, CancellationToken clt = default)
8988
{
90-
switch (storageLocation)
89+
try
90+
{
91+
switch (storageLocation)
92+
{
93+
case StorageLocation.BrowserLocal:
94+
await _jsRuntime.InvokeVoidAsync("localStorage.removeItem", clt, storageKey);
95+
break;
96+
case StorageLocation.BrowserSession:
97+
await _jsRuntime.InvokeVoidAsync("sessionStorage.removeItem", clt, storageKey);
98+
break;
99+
default:
100+
_pageCache.Remove(storageKey);
101+
break;
102+
}
103+
}
104+
catch (Exception ex)
91105
{
92-
case StorageLocation.BrowserLocal:
93-
await _jsRuntime.InvokeVoidAsync("localStorage.removeItem", clt, storageKey);
94-
break;
95-
case StorageLocation.BrowserSession:
96-
await _jsRuntime.InvokeVoidAsync("sessionStorage.removeItem", clt, storageKey);
97-
break;
98-
default:
99-
_pageCache.Remove(storageKey);
100-
break;
106+
ConsoleHelper.WriteLine("Exception in RemoveItem", ex);
101107
}
102108
}
103-
}
109+
}

0 commit comments

Comments
 (0)