BrowserCache.Extensions 0.1.0-alpha

This is a prerelease version of BrowserCache.Extensions.
There is a newer version of this package available.
See the version list below for details.
dotnet add package BrowserCache.Extensions --version 0.1.0-alpha
                    
NuGet\Install-Package BrowserCache.Extensions -Version 0.1.0-alpha
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BrowserCache.Extensions" Version="0.1.0-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BrowserCache.Extensions" Version="0.1.0-alpha" />
                    
Directory.Packages.props
<PackageReference Include="BrowserCache.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BrowserCache.Extensions --version 0.1.0-alpha
                    
#r "nuget: BrowserCache.Extensions, 0.1.0-alpha"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BrowserCache.Extensions@0.1.0-alpha
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BrowserCache.Extensions&version=0.1.0-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=BrowserCache.Extensions&version=0.1.0-alpha&prerelease
                    
Install as a Cake Tool

BrowserCacheExtensions

BrowserCacheExtensions is a collection of extensions designed to cache non-confidential data in the browser using popular libraries like Blazored LocalStorage. Future plans include extending support to Blazor.IndexedDB.

Usage

LocalStorageAsyncExtensions

The LocalStorageAsyncExtensions class provides asynchronous methods for caching data. Here's how to use it:

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<MyDataType> FetchDataAsync()
    {
        // Fetch or generate data
        return await Task.FromResult(new MyDataType());
    }
}

LocalStorageSyncExtensions

The LocalStorageSyncExtensions class offers synchronous methods for caching data:

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

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.5 128 2/16/2025
0.2.4 107 2/15/2025
0.2.1 113 2/12/2025
0.2.0-alpha.4 81 2/11/2025
0.1.0-alpha 88 2/11/2025
0.0.1 182 4/15/2025