Created
June 6, 2021 15:42
-
-
Save manoj-choudhari-git/9817278c66111d78783915aad518fd9b to your computer and use it in GitHub Desktop.
.NET Core Web API - Size limits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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