NetcodeHub.Packages.Wrappers.OutputCache 1.0.1

dotnet add package NetcodeHub.Packages.Wrappers.OutputCache --version 1.0.1                
NuGet\Install-Package NetcodeHub.Packages.Wrappers.OutputCache -Version 1.0.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="NetcodeHub.Packages.Wrappers.OutputCache" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetcodeHub.Packages.Wrappers.OutputCache --version 1.0.1                
#r "nuget: NetcodeHub.Packages.Wrappers.OutputCache, 1.0.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.
// Install NetcodeHub.Packages.Wrappers.OutputCache as a Cake Addin
#addin nuget:?package=NetcodeHub.Packages.Wrappers.OutputCache&version=1.0.1

// Install NetcodeHub.Packages.Wrappers.OutputCache as a Cake Tool
#tool nuget:?package=NetcodeHub.Packages.Wrappers.OutputCache&version=1.0.1                

Install the package

NetcodeHub.Packages.Wrappers.OutputCache

Register Service with OutputCache

builder.Services.AddNetcodeHubOutputCache();

Register Middleware pipeline: follow this order when consuming in Client / Wasm

//This should be the order
app.UseCors();
app.UseAuthorization();
app.UseNetcodeHubOutputCache();
app.MapControllers();
app.Run();

For Client Consumption like WASM, Register CORS

builder.Services.AddCors(options =>
{
    options.AddDefaultPolicy(builder =>
    {
        builder.WithOrigins("https://localhost:7208")
        .AllowAnyHeader()
        .AllowAnyMethod();
    });
});

Inject the INetcodeHubOutputCache in Somewhere

namespace API2.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController(INetcodeHubOutputCache netcodeHubOutputCache) : ControllerBase
    {
          private static readonly string[] Summaries = new[]
          {
              "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
          };
  
          //Read, Write / Remove cache
          [HttpGet(Name = "GetWeatherForecast")]
          public async Task<ActionResult<IEnumerable<WeatherForecast>>> Get()
          {
              var weatherList = await netcodeHubOutputCache.GetCacheAsync("weatherData", CancellationToken.None);
              if (!string.IsNullOrEmpty(weatherList))
                  return Ok(JsonSerializer.Deserialize<IEnumerable<WeatherForecast>>(weatherList));
  
              await Task.Delay(2000);
              var data = Enumerable.Range(1, 15).Select(index => new WeatherForecast
              {
                  Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                  TemperatureC = Random.Shared.Next(-20, 55),
                  Summary = Summaries[Random.Shared.Next(Summaries.Length)]
              }).ToArray();
  
              await netcodeHubOutputCache.SetCacheAsync(
                                              "weatherData",
                                               JsonSerializer.Serialize(data),
                                               null,
                                               TimeSpan.FromSeconds(30),
                                               CancellationToken.None);
  
              return Ok(data);
          }
  
          [HttpGet("clear-cache")]
          public async Task<IActionResult> ClearCache()
          {
              await netcodeHubOutputCache.RemoveCacheAsync("weatherData", CancellationToken.None);
              return NoContent();
          }
      }
  }

Key factors

Set

 await netcodeHubOutputCache.SetCacheAsync("weatherData", JsonSerializer.Serialize(data), null, TimeSpan.FromSeconds(30), CancellationToken.None);

Get

 var data = await netcodeHubOutputCache.GetCacheAsync("weatherData", CancellationToken.None);

Remove

  await netcodeHubOutputCache.RemoveCacheAsync("weatherData", CancellationToken.None);

Here's a follow-up section to encourage engagement and support for Netcode-Hub:

🌟 Get in touch with Netcode-Hub! 📫

  1. GitHub: Explore Repositories 🌐
  2. Twitter: Stay Updated 🐦
  3. Facebook: Connect Here 📘
  4. LinkedIn: Professional Network 🔗
  5. Email: Email: business.netcodehub@gmail.com 📧

☕️ If you've found value in Netcode-Hub's work, consider supporting the channel with a coffee!

  1. Buy Me a Coffee: Support Netcode-Hub ☕️
  2. Patreon: Support on Patreon 🌟
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.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
1.0.1 135 5/31/2024

Initial release featuring core caching functionalities:
SetCacheAsync: Asynchronously sets data in the cache with support for expiration and tagging.
GetCacheAsync: Asynchronously retrieves cached data based on the specified cache key.
RemoveCacheAsync: Asynchronously removes cached data associated with the specified cache key.
Thread-safe implementation using SemaphoreSlim to prevent race conditions during cache operations.