Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/9817278c66111d78783915aad518fd9b to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/9817278c66111d78783915aad518fd9b to your computer and use it in GitHub Desktop.
.NET Core Web API - Size limits
// In Startup.ConfigureServices,
services.AddMemoryCache(options =>
{
// Overall 1024 size (no unit)
options.SizeLimit = 1024;
});
// In WeatherForecastController.cs
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
IEnumerable<WeatherForecast> weatherForecastCollection = null;
if (_memoryCache.TryGetValue(weatherForecastKey, out weatherForecastCollection))
{
return weatherForecastCollection;
}
weatherForecastCollection = GetWeatherForecast();
var cacheOptions = new MemoryCacheEntryOptions()
.SetSize(1) // Set size of current item
.SetSlidingExpiration(TimeSpan.FromSeconds(10))
.SetAbsoluteExpiration(TimeSpan.FromSeconds(30));
_memoryCache.Set(weatherForecastKey, weatherForecastCollection, cacheOptions);
return weatherForecastCollection;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment