Drogecode.Blazor.ExpireStorage 0.1.1

dotnet add package Drogecode.Blazor.ExpireStorage --version 0.1.1
                    
NuGet\Install-Package Drogecode.Blazor.ExpireStorage -Version 0.1.1
                    
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="Drogecode.Blazor.ExpireStorage" Version="0.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Drogecode.Blazor.ExpireStorage" Version="0.1.1" />
                    
Directory.Packages.props
<PackageReference Include="Drogecode.Blazor.ExpireStorage" />
                    
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 Drogecode.Blazor.ExpireStorage --version 0.1.1
                    
#r "nuget: Drogecode.Blazor.ExpireStorage, 0.1.1"
                    
#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 Drogecode.Blazor.ExpireStorage@0.1.1
                    
#: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=Drogecode.Blazor.ExpireStorage&version=0.1.1
                    
Install as a Cake Addin
#tool nuget:?package=Drogecode.Blazor.ExpireStorage&version=0.1.1
                    
Install as a Cake Tool

Nuget version Nuget downloads

Drogecode.Blazor.ExpireStorage

Adds a wrapper on top of Blazored.LocalStorage and Blazored.SessionStorage to expire items from localstorage and sessionstorage after a specified time.

Installing

To install the package add the following line to you csproj file replacing x.x.x with the latest version number (found at the top of this file):

<PackageReference Include="Drogecode.Blazor.ExpireStorage" Version="x.x.x" />

You can also install via the .NET CLI with the following command:

dotnet add package Drogecode.Blazor.ExpireStorage

If you're using Visual Studio you can also install via the built in NuGet package manager.

Setup

You will need to register the expire storage services with the service collection in your Startup.cs file in Blazor Server.

public void ConfigureServices(IServiceCollection services)
{
    services.AddExpireStorage();
}

Or in your Program.cs file in Blazor WebAssembly.

public static async Task Main(string[] args)
{
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    builder.RootComponents.Add<App>("app");

    builder.Services.AddExpireStorage();

    await builder.Build().RunAsync();
}

If you use Blazored.LocalStorage or Blazored.SessionStorage with configuration those will need to be registered before Drogecode.Blazor.ExpireStorage.

Registering services as Singleton - Blazor WebAssembly ONLY

99% of developers will want to register Blazored LocalStorage using the method described above. However, in some very specific scenarios developer may have a need to register services as Singleton as apposed to Scoped. This is possible by using the following method:

builder.Services.AddExpireStorageAsSingleton();

This method will not work with Blazor Server applications as Blazor's JS interop services are registered as Scoped and cannot be injected into Singletons.

Usage (Blazor WebAssembly)

example

@inject Drogecode.Blazor.ExpireStorage.IExpireStorageService storageService

@code {
    
    public async Task<YourObjectResponse?> GetDayItemsAsync(DateRange dateRange, Guid userId, CancellationToken clt)
    {
        var cacheKey = "CACHE_KEY_HERE"
        var response = await storageService.CachedRequestAsync(cacheKey),
            async () => await apiClient.GetItemsAsync(),
            new CachedRequest{CachedAndReplace = true}, clt);
        return response;
    }

}

Options

CachedRequest

You can give optional settings to the CachedRequest object.

  • OneCallPerSession - If true, the result will be returned from sessionstorage if it is not expired.
  • OneCallPerCache - If true, the result will be returned from localstorage if it is not expired.
  • IgnoreCache - If true, never return a cached result.
  • ExpireLocalStorage - The time to expire the result in localstorage. Default is 7 days.
  • ExpireSessionStorage - The time to expire the result in sessionstorage. Default is 15 minutes.
  • CachedAndReplace - If true, The cached result will be returned and the cache will be refreshed for the next call.
  • RetryOnJsonException - If true, If a JSON exception occurs, the cache will be cleared and the request will be retried.

Global settings

On, for example, MainLayout.razor.cs, you can set the Postfix to be used for all requests. This is useful if you have multiple users using the same app from the same browser.

ExpireStorageService.Postfix = userId.ToString();

ICacheableResponse

If a response object implements ICacheableResponse, the FromCache property will be set to true if the result was retrieved from cache.

public class YourObjectResponse : ICacheableResponse
{
    ...
    public bool FromCache { get; set; }
    ...
}
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in 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.1.1 278 11/15/2025
0.1.0 237 11/3/2025