Created
June 6, 2021 15:26
-
-
Save manoj-choudhari-git/07d271ddc6f18122df3c187a5f4e923e to your computer and use it in GitHub Desktop.
.NET Core Web API - Sliding and Absolute Expirations both specified on an item
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
[HttpGet] | |
public IEnumerable<WeatherForecast> Get() | |
{ | |
IEnumerable<WeatherForecast> weatherForecastCollection = null; | |
if (_memoryCache.TryGetValue(weatherForecastKey, out weatherForecastCollection)) | |
{ | |
return weatherForecastCollection; | |
} | |
weatherForecastCollection = GetWeatherForecast(); | |
// Sliding + Absolute Expiration | |
var cacheOptions = new MemoryCacheEntryOptions() | |
.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