From 70b82ae0633cf3949ba6765b91caff5dd1447173 Mon Sep 17 00:00:00 2001 From: Stefan Date: Tue, 8 Dec 2020 18:37:09 +0100 Subject: [PATCH] Add example with set extension methods to memory cache usage (#20850) * Add examples with Set extension methods * Just one example * Add more description. --- aspnetcore/performance/caching/memory.md | 3 +++ .../Controllers/HomeController.cs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/aspnetcore/performance/caching/memory.md b/aspnetcore/performance/caching/memory.md index d826a68d83..0500a05df2 100644 --- a/aspnetcore/performance/caching/memory.md +++ b/aspnetcore/performance/caching/memory.md @@ -67,6 +67,9 @@ The current time and the cached time are displayed: [!code-cshtml[](memory/3.0sample/WebCacheSample/Views/Home/Cache.cshtml)] +The following code uses the [Set](/dotnet/api/microsoft.extensions.caching.memory.cacheextensions.set#Microsoft_Extensions_Caching_Memory_CacheExtensions_Set__1_Microsoft_Extensions_Caching_Memory_IMemoryCache_System_Object___0_System_TimeSpan_) extension method to cache data for a relative time without creating the `MemoryCacheEntryOptions` object. +[!code-csharp[](memory/3.0sample/WebCacheSample/Controllers/HomeController.cs?name=snippet_set)] + The cached `DateTime` value remains in the cache while there are requests within the timeout period. The following code uses [GetOrCreate](/dotnet/api/microsoft.extensions.caching.memory.cacheextensions.getorcreate#Microsoft_Extensions_Caching_Memory_CacheExtensions_GetOrCreate__1_Microsoft_Extensions_Caching_Memory_IMemoryCache_System_Object_System_Func_Microsoft_Extensions_Caching_Memory_ICacheEntry___0__) and [GetOrCreateAsync](/dotnet/api/microsoft.extensions.caching.memory.cacheextensions.getorcreateasync#Microsoft_Extensions_Caching_Memory_CacheExtensions_GetOrCreateAsync__1_Microsoft_Extensions_Caching_Memory_IMemoryCache_System_Object_System_Func_Microsoft_Extensions_Caching_Memory_ICacheEntry_System_Threading_Tasks_Task___0___) to cache data. diff --git a/aspnetcore/performance/caching/memory/3.0sample/WebCacheSample/Controllers/HomeController.cs b/aspnetcore/performance/caching/memory/3.0sample/WebCacheSample/Controllers/HomeController.cs index 1c9742cf86..7394e7c99b 100644 --- a/aspnetcore/performance/caching/memory/3.0sample/WebCacheSample/Controllers/HomeController.cs +++ b/aspnetcore/performance/caching/memory/3.0sample/WebCacheSample/Controllers/HomeController.cs @@ -79,6 +79,25 @@ public class HomeController : Controller } #endregion + #region snippet_set + public IActionResult SetCacheRelativeExpiration() + { + DateTime cacheEntry; + + // Look for cache key. + if (!_cache.TryGetValue(CacheKeys.Entry, out cacheEntry)) + { + // Key not in cache, so get data. + cacheEntry = DateTime.Now; + + // Save data in cache and set the relative expiration time to one day + _cache.Set(CacheKeys.Entry, cacheEntry, TimeSpan.FromDays(1)); + } + + return View("Cache", cacheEntry); + } + #endregion + public IActionResult CacheRemove() { _cache.Remove(CacheKeys.Entry);