Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jjosh102/browser-cache-extensions
BrowserCacheExtensions is a collection of extensions designed to cache non-confidential data in the browser.
https://github.com/jjosh102/browser-cache-extensions
blazor blazor-webassembly csahrp localstorage show
Last synced: 4 days ago
JSON representation
BrowserCacheExtensions is a collection of extensions designed to cache non-confidential data in the browser.
- Host: GitHub
- URL: https://github.com/jjosh102/browser-cache-extensions
- Owner: jjosh102
- Created: 2025-02-10T12:18:08.000Z (9 days ago)
- Default Branch: master
- Last Pushed: 2025-02-11T01:37:14.000Z (8 days ago)
- Last Synced: 2025-02-11T02:26:38.711Z (8 days ago)
- Topics: blazor, blazor-webassembly, csahrp, localstorage, show
- Language: C#
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# BrowserCacheExtensions
[](https://www.nuget.org/packages/BrowserCache.Extensions)
[](https://www.nuget.org/packages/BrowserCache.Extensions)BrowserCacheExtensions is a collection of extensions designed to cache non-confidential data in the browser using popular libraries like [Blazored LocalStorage](https://github.com/Blazored/LocalStorage). Future plans include extending support to [Blazor.IndexedDB](https://github.com/wtulloch/Blazor.IndexedDB).
## Installing
To install the package add the following line inside your csproj file with the latest version.
```
```
An alternative is to install via the .NET CLI with the following command:
```
dotnet add package BrowserCache.Extensions
```## Usage
### LocalStorageAsyncExtensions
The `LocalStorageAsyncExtensions` class provides asynchronous methods for caching data. Here's how to use it:
```csharp
using Blazored.LocalStorage;
using BrowserCache.Extensions.LocalStorage;
using System;
using System.Threading.Tasks;public class Example
{
private readonly ILocalStorageService _localStorageService;public Example(ILocalStorageService localStorageService)
{
_localStorageService = localStorageService;
}public async Task UseCacheAsync()
{
string key = "cachedData";
TimeSpan timeToLive = TimeSpan.FromMinutes(10);var data = await _localStorageService.GetOrCreateCacheAsync(
key,
timeToLive,
async () => await FetchDataAsync()
);// Use 'data' as needed
}private async Task FetchDataAsync()
{
// Fetch or generate data
return await Task.FromResult(new MyDataType());
}
}
```### LocalStorageSyncExtensions
The `LocalStorageSyncExtensions` class offers synchronous methods for caching data:
```csharp
using Blazored.LocalStorage;
using BrowserCache.Extensions.LocalStorage;
using System;public class Example
{
private readonly ISyncLocalStorageService _localStorageService;public Example(ISyncLocalStorageService localStorageService)
{
_localStorageService = localStorageService;
}public void UseCache()
{
string key = "cachedData";
TimeSpan timeToLive = TimeSpan.FromMinutes(10);var data = _localStorageService.GetOrCreateCache(
key,
timeToLive,
() => FetchData()
);// Use 'data' as needed
}private MyDataType FetchData()
{
// Fetch or generate data
return new MyDataType();
}
}
```## Note
If you prefer not to rely on this library, you can copy the extension methods directly into your project and modify them as needed to suit your requirements.## Disclaimer
Use only this library for caching non sensitive data.
If you are working with highly private and confidential data , you should not be storing this data in your client's browser.# License
MIT License