Skip to content

Tharga/Cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tharga Cache

GitHub repo Issues NuGet Nuget License

Get Started

Register the service

var builder = WebApplication.CreateBuilder(args);

builder.Services.RegisterCache();

Inject to service and start using

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    private readonly ITimeToLiveCache _ttlCache;

    public WeatherForecastController(ITimeToLiveCache ttlCache)
    {
        _ttlCache = ttlCache;
    }

    [HttpGet]
    public async Task<IActionResult> GetSame()
    {
        var response = await _ttlCache.GetAsync("MyCacheKey", LoadWeatherData, TimeSpan.FromSeconds(30));
        return Ok(response);
    }

    private static Task<WeatherForecast[]> LoadWeatherData()
    {
        var data = Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), TemperatureC = Random.Shared.Next(-20, 55) }).ToArray();
        return Task.FromResult(data);
    }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published