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/07d271ddc6f18122df3c187a5f4e923e to your computer and use it in GitHub Desktop.
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
[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