Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
.NET Core Web API - Memory Cache Usage
// Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Add memory cache dependencies
services.AddMemoryCache();
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApiInMemoryCaching", Version = "v1" });
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApiInMemoryCaching v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
// WeatherForecastController.cs
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild",
"Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
private readonly IMemoryCache _memoryCache;
private readonly string weatherForecastKey = "weatherForecastKey";
public WeatherForecastController(ILogger<WeatherForecastController> logger, IMemoryCache memoryCache)
{
_logger = logger;
_memoryCache = memoryCache;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
IEnumerable<WeatherForecast> weatherForecastCollection = null;
// If found in cache, return cached data
if (_memoryCache.TryGetValue(weatherForecastKey, out weatherForecastCollection))
{
return weatherForecastCollection;
}
// If not found, then calculate response
weatherForecastCollection = GetWeatherForecast();
// Set cache options
var cacheOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromSeconds(30));
/ Set object in cache
_memoryCache.Set(weatherForecastKey, weatherForecastCollection, cacheOptions);
return weatherForecastCollection;
}
private static IEnumerable<WeatherForecast> GetWeatherForecast()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment