{"id":28464,"date":"2019-07-02T00:00:00","date_gmt":"2019-07-01T22:00:00","guid":{"rendered":"https:\/\/blexin.com\/in-memory-caching-in-asp-net-core\/"},"modified":"2021-05-20T18:57:17","modified_gmt":"2021-05-20T16:57:17","slug":"in-memory-caching-in-asp-net-core","status":"publish","type":"post","link":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/","title":{"rendered":"In-memory caching in ASP.NET Core"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"608\" data-attachment-id=\"28457\" data-permalink=\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/attachment\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57-2\/\" data-orig-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=1024%2C608&amp;ssl=1\" data-orig-size=\"1024,608\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"e9e079d5-c58f-4ce0-b0d1-505d6fd97b57\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=300%2C178&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=1024%2C608&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?resize=1024%2C608&#038;ssl=1\" alt=\"\" class=\"wp-image-28457\" srcset=\"https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png 1024w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57-980x582.png 980w, https:\/\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57-480x285.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1024px, 100vw\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">I believe it has happened to everyone, in our job, to receive requests from clients, or feedback from users of our applications, to improve responsiveness.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If using&nbsp;<strong>best practices<\/strong>&nbsp;when we write code it\u2019s not enough, we surely need to use the caching to nudge our applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Caching consists in storing somewhere those information, that change less frequently. The frequency is a business requirement of our application.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we will see what does ASP.NET Core make avalable for caching.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">IMemoryCache and IDistributedCache<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These two interfaces represent the&nbsp;<em>built-in<\/em>&nbsp;mechanism for caching in .NET Core. All the other techniques, you may have heard about, are implementations of these two interfaces. In this article, we will look in detail at the<em>&nbsp;in-memory cache<\/em>, whereas the distributed cache will be examined in a future article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Enable in-memory caching in ASP.NET Core<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The ASP.NET in-memory cache is a feature we can incorporate in our application using the method&nbsp;<strong>ConfigureServices<\/strong>. You can enable the<em>&nbsp;in-memory cache<\/em>&nbsp;in the&nbsp;<strong>Startup&nbsp;<\/strong>class as shown in the code snippet below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic void ConfigureServices(IServiceCollection services)\n{\n\u00a0\u00a0\u00a0\u00a0services.AddMvc();\n\u00a0\u00a0\u00a0\u00a0services.AddMemoryCache();\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<strong>AddMemoryCache<\/strong>&nbsp;method allows us to register the&nbsp;<em>IMemoryCache<\/em>&nbsp;interface that, as mentioned above, is the basis to be used for the caching. Below we see the definition of the interface in the framework:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic interface IMemoryCache : IDisposable\n{\n\u00a0\u00a0\u00a0\u00a0bool TryGetValue(object key, out object value);\n\u00a0\u00a0\u00a0\u00a0ICacheEntry CreateEntry(object key);\n\u00a0\u00a0\u00a0\u00a0void Remove(object key);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Methods present in the interface are not the only ones available to work with the cache: there are different extensions that enrich the available APIs and greatly facilitate their use, as we will see later. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic static class CacheExtensions\n{\n\u00a0\u00a0\u00a0\u00a0public static TItem Get&lt;titem&gt;(this IMemoryCache cache, object key);\n\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0public static TItem Set&lt;titem&gt;(this IMemoryCache cache, object key, TItem value, MemoryCacheEntryOptions options);\n\u00a0\n\u00a0\u00a0\u00a0\u00a0public static bool TryGetValue&lt;titem&gt;(this IMemoryCache cache, object key, out TItem value);\n\u00a0\u00a0\u00a0\u00a0...\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Once registered, the interface is injectable in class constructors where we want to use it, as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nprivate IMemoryCache cache;\npublic MyCacheController(IMemoryCache cache)\n{\u00a0\u00a0\u00a0 \n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.cache = cache;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">In the section that follows, we will look at how we can work with the cache API in ASP.NET Core to store and retrieve objects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Store and retrieve items using IMemoryCache<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To write an object using the&nbsp;<strong>IMemoryCache<\/strong>&nbsp;interface, use the&nbsp;<strong>Set&lt;T&gt;()&nbsp;<\/strong>method as shown in the following snippet.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;HttpGet]\npublic string Get()\n{\n\u00a0\u00a0\u00a0\u00a0cache.Set(\u201cMyKey\u201d, DateTime.Now.ToString());\n\u00a0\u00a0\u00a0\u00a0return \u201cThis is a test method...\u201d;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">This method accepts two parameters, the first is the key, with which the cached object will be identified, the second parameter is the value we want to store. To retrieve an object from the cache the&nbsp;<strong>Get&lt;T&gt;()<\/strong>&nbsp;method is used as shown with the next snippet.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;HttpGet(\u201c{key}\u201d)]\npublic string Get(string key)\n{\n\u00a0\u00a0\u00a0\u00a0return cache.Get&lt;string&gt;(key);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">If we are not sure that a specific key is present in our cache, the TryGetValue() method comes to help: it returns a Boolean, that indicates the existence or non-existence of the requested key.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Below is how you can modify the&nbsp;<em>Get()&nbsp;<\/em>method using the&nbsp;<em>TryGetValue<\/em>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;HttpGet(\u201c{key}\u201d)]\npublic string Get(string key)\n{\n\u00a0\u00a0\u00a0\u00a0string obj;\n\u00a0\u00a0\u00a0\u00a0if (!cache.TryGetValue&amp;lt;string&gt;(key, out obj))\n\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0obj = DateTime.Now.ToString();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cache.Set&amp;lt;string&gt;(key, obj);\n\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0return obj;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Another method available is the&nbsp;<strong>GetOrCreate()<\/strong>&nbsp;method, that verifies the existence of the required key, otherwise, the method creates it for you.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;HttpGet(\u201c{key}\u201d)]\npublic string Get(string key)\n{\n\u00a0\u00a0\u00a0\u00a0return cache.GetOrCreate&amp;lt;string&gt;(\u201ckey\u201d,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0cacheEntry =&gt; {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return DateTime.Now.ToString();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0});\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">How to set expiration policies on cached data in ASP.NET&nbsp;Core<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When we store objects with IMemoryCache, the class&nbsp;<strong>MemoryCacheEntryOptions<\/strong>&nbsp;provides us various techniques to manage the expiration of cached data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can indicate a fixed time after which a certain key expires (<strong>absolute expiry<\/strong>), or it can expire if it is not accessed after a certain time (<strong>sliding expiry<\/strong>). Furthermore, there is the possibility to create dependencies between cached objects using the&nbsp;<strong>Expiration Token<\/strong>. Here some examples<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/absolute expiration using TimeSpan\n_cache.Set(&quot;key&quot;, item, TimeSpan.FromDays(1));\n\u00a0\n\/\/absolute expiration using DateTime\n_cache.Set(&quot;key&quot;, item, new DateTime(2020, 1, 1));\n\u00a0\n\/\/sliding expiration (scade se non viene letta per 7 giorni)\n_cache.Set(&quot;key&quot;, item, new MemoryCacheEntryOptions\n{\n\u00a0\u00a0\u00a0\u00a0SlidingExpiration = TimeSpan.FromDays(7)\n});\n\u00a0\n\/\/use both absolute and sliding expiration\n_cache.Set(&quot;key&quot;, item, new MemoryCacheEntryOptions\n{\n\u00a0\u00a0\u00a0\u00a0AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(30),\n\u00a0\u00a0\u00a0\u00a0SlidingExpiration = TimeSpan.FromDays(7)\n});\n\u00a0\n\/\/ This method adds a trigger to refresh the data from background\nprivate void UpdateReset()\n{\n\u00a0\u00a0\u00a0\u00a0var mo = new MemoryCacheEntryOptions();\n\u00a0\u00a0\u00a0\u00a0mo.RegisterPostEvictionCallback(RefreshAllPlacessCache_PostEvictionCallback);\n\u00a0\u00a0\u00a0\u00a0mo.AddExpirationToken(new CancellationChangeToken(new CancellationTokenSource(TimeSpan.FromMinutes(35)).Token));\n\u00a0\u00a0\u00a0\u00a0Cache.Set(CACHE_KEY_PLACES_RESET, DateTime.Now, mo);\n}\n\u00a0\n\/\/ Method triggered by the cancellation token that triggers the PostEvictionCallBack\nprivate async void RefreshAllPlacesCache_PostEvictionCallback(object key, object value, EvictionReason reason, object state)\n{\n\u00a0\u00a0\u00a0\u00a0\/\/ Regenerate a set of updated data\n\u00a0\u00a0\u00a0\u00a0var places = await GetLongGeneratingData();\n\u00a0\u00a0\u00a0\u00a0Cache.Set(CACHE_KEY_PLACES, places, TimeSpan.FromMinutes(40));\n\u00a0\n\u00a0\u00a0\u00a0\u00a0\/\/ Re-set the cache to be reloaded in 35min\n\u00a0\u00a0\u00a0\u00a0UpdateReset();\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Cache callbacks<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Another interesting feature available with&nbsp;<strong>MemoryCacheEntryOptions<\/strong>&nbsp;class is the one that permits us to register callbacks, to be executed when an item is removed from the cache.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nMemoryCacheEntryOptions cacheOption = new MemoryCacheEntryOptions()\u00a0 \n{\u00a0 \n\u00a0\u00a0\u00a0\u00a0AbsoluteExpirationRelativeToNow = (DateTime.Now.AddMinutes(1) - DateTime.Now),\u00a0 \n};\u00a0 \ncacheOption.RegisterPostEvictionCallback(\u00a0 \n\u00a0\u00a0\u00a0\u00a0(key, value, reason, substate) =&gt;\u00a0 \n\u00a0\u00a0\u00a0\u00a0{\u00a0 \n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Console.Write(&quot;Cache expired!&quot;);\u00a0 \n\u00a0\u00a0\u00a0\u00a0}); \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Cache Tag Helper<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We have seen so far the use of APIs made available by .NET Core, to be able to manually write and read item in the cache using the IMemoryCache interface directly. There are also other implementations of this interface, that can be very useful. For example, in the Web environment, if we use the .NET Core MVC framework, we can store parts of pages using the&nbsp;<strong>helper cache tag<\/strong>. It\u2019s really simple to use: you can wrap part of a view in cache tags to enable caching:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&lt;cache&gt;\n&lt;p&gt;Ora: @DateTime.Now&lt;\/p&gt;\n&lt;\/cache&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">For each subsequent request of the page, that contains this tag, the body of the paragraph will be used from the cache. You can easily check the behavior, if you put it on a page and observe its output. Of course, the way we used it is for example purposes only, but you can appreciate its capabilities when you try to render a piece of page, that requires a lot of resources. One obvious candidate for caching is a view&nbsp;<a href=\"https:\/\/www.blexin.com\/en-US\/Article\/Blog\/Kestrel-build-me-up-31\">component<\/a>&nbsp;call<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&lt;cache expires-on=&quot;@TimeSpan.FromSeconds(600)&quot;&gt;\n\u00a0\u00a0\u00a0\u00a0@await Component.InvokeAsync(&quot;BlogPosts&quot;, new { tag = &quot;popular&quot; })\n&lt;\/cache&gt;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">In the previous snippet, you can also see how to manage the expiring period of the object in cache, through the attribute&nbsp;<strong>expires-on<\/strong>. There are two other alternatives:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>expires-after: to be evaluated with a TimeSpan to indicate a period of time, after which, the content must be regenerated;<\/li><li>expires-sliding: a TimeSpan that indicates a period of inactivity should also be used. Each time the content is read from the cache, its removal is postponed.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Another customizable aspect concerns the possibility to configure the caching criterion. We may need to update our cached object based on some variables. Some requirements are covered by the&nbsp;<strong>vary-by-<\/strong>&nbsp;attributes listed below:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>vary-by-route: is enhanced with the name of a&nbsp;<strong>route parameter<\/strong>, for example id, to indicate that the content must be regenerated when the indicated property change;<\/li><li>vary-by-query: the content is generated and cached when the<strong>&nbsp;querystring key<\/strong>&nbsp;is changed;<\/li><li>vary-by-user: must be set to true when we display specific data for the logged in user, such as the profile box containing the name and photo;<\/li><li>vary-by-header: to vary the cache based on an HTTP request header, such as &#8220;Accept-Language&#8221; if we are using it to display language content;<\/li><li>vary-by-cookie: allows you to change the cache based on the content of a cookie, whose name we must indicate.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It is possible to use one or more vary-by- attributes to carry out advanced caching policies, but, taking up a famous quote, \u201c<em>With great power, comes great responsibility<\/em>\u201d.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Using caching in-memory allows you to store data in the server&#8217;s memory and helps us to improve application performances by removing unnecessary requests to external data sources. As we have seen, it is very simple to use.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I remind you that this approach cannot be used when your app is hosted on multiple servers or in a cloud hosting environment. This aspect will be examined in the next article in which we will talk about distributed caching.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">See you next!<\/p>\n\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s see how to optimize ASP.NET Core application performance with caching<\/p>\n","protected":false},"author":196716246,"featured_media":28457,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"off","_et_pb_old_content":"","_et_gb_content_width":"","_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_crdt_document":"","inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false},"categories":[688637524],"tags":[688637416,688637384],"class_list":["post-28464","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-en","tag-asp-net-core-en","tag-c-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>In-memory caching in ASP.NET Core - Blexin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"In-memory caching in ASP.NET Core - Blexin\" \/>\n<meta property=\"og:description\" content=\"Let&#039;s see how to optimize ASP.NET Core application performance with caching\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/\" \/>\n<meta property=\"og:site_name\" content=\"Blexin\" \/>\n<meta property=\"article:published_time\" content=\"2019-07-01T22:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-20T16:57:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=1024%2C608&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"608\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Francesco de Vicariis\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Francesco de Vicariis\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/\"},\"author\":{\"name\":\"Francesco de Vicariis\",\"@id\":\"https:\/\/blexin.com\/en\/#\/schema\/person\/6f8514ed8b0d3be31369ca5436c4781f\"},\"headline\":\"In-memory caching in ASP.NET Core\",\"datePublished\":\"2019-07-01T22:00:00+00:00\",\"dateModified\":\"2021-05-20T16:57:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/\"},\"wordCount\":1087,\"image\":{\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=1024%2C608&ssl=1\",\"keywords\":[\"Asp.net core\",\"C#\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/\",\"url\":\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/\",\"name\":\"In-memory caching in ASP.NET Core - Blexin\",\"isPartOf\":{\"@id\":\"https:\/\/blexin.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=1024%2C608&ssl=1\",\"datePublished\":\"2019-07-01T22:00:00+00:00\",\"dateModified\":\"2021-05-20T16:57:17+00:00\",\"author\":{\"@id\":\"https:\/\/blexin.com\/en\/#\/schema\/person\/6f8514ed8b0d3be31369ca5436c4781f\"},\"breadcrumb\":{\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=1024%2C608&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=1024%2C608&ssl=1\",\"width\":1024,\"height\":608},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blexin.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"In-memory caching in ASP.NET Core\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blexin.com\/en\/#website\",\"url\":\"https:\/\/blexin.com\/en\/\",\"name\":\"Blexin\",\"description\":\"Con noi \u00e8 semplice\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blexin.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blexin.com\/en\/#\/schema\/person\/6f8514ed8b0d3be31369ca5436c4781f\",\"name\":\"Francesco de Vicariis\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blexin.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b3a3164fd0b28d429cd427aafae38a687a41a250a2bccf4ab3b0744138afd64e?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b3a3164fd0b28d429cd427aafae38a687a41a250a2bccf4ab3b0744138afd64e?s=96&d=identicon&r=g\",\"caption\":\"Francesco de Vicariis\"},\"url\":\"https:\/\/blexin.com\/en\/author\/francesco-devicariisblexin-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"In-memory caching in ASP.NET Core - Blexin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/","og_locale":"en_US","og_type":"article","og_title":"In-memory caching in ASP.NET Core - Blexin","og_description":"Let's see how to optimize ASP.NET Core application performance with caching","og_url":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/","og_site_name":"Blexin","article_published_time":"2019-07-01T22:00:00+00:00","article_modified_time":"2021-05-20T16:57:17+00:00","og_image":[{"width":1024,"height":608,"url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=1024%2C608&ssl=1","type":"image\/png"}],"author":"Francesco de Vicariis","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Francesco de Vicariis","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#article","isPartOf":{"@id":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/"},"author":{"name":"Francesco de Vicariis","@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/6f8514ed8b0d3be31369ca5436c4781f"},"headline":"In-memory caching in ASP.NET Core","datePublished":"2019-07-01T22:00:00+00:00","dateModified":"2021-05-20T16:57:17+00:00","mainEntityOfPage":{"@id":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/"},"wordCount":1087,"image":{"@id":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=1024%2C608&ssl=1","keywords":["Asp.net core","C#"],"articleSection":["Blog"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/","url":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/","name":"In-memory caching in ASP.NET Core - Blexin","isPartOf":{"@id":"https:\/\/blexin.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#primaryimage"},"image":{"@id":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=1024%2C608&ssl=1","datePublished":"2019-07-01T22:00:00+00:00","dateModified":"2021-05-20T16:57:17+00:00","author":{"@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/6f8514ed8b0d3be31369ca5436c4781f"},"breadcrumb":{"@id":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#primaryimage","url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=1024%2C608&ssl=1","contentUrl":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=1024%2C608&ssl=1","width":1024,"height":608},{"@type":"BreadcrumbList","@id":"https:\/\/blexin.com\/en\/blog-en\/in-memory-caching-in-asp-net-core\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blexin.com\/en\/"},{"@type":"ListItem","position":2,"name":"In-memory caching in ASP.NET Core"}]},{"@type":"WebSite","@id":"https:\/\/blexin.com\/en\/#website","url":"https:\/\/blexin.com\/en\/","name":"Blexin","description":"Con noi \u00e8 semplice","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blexin.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/6f8514ed8b0d3be31369ca5436c4781f","name":"Francesco de Vicariis","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blexin.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b3a3164fd0b28d429cd427aafae38a687a41a250a2bccf4ab3b0744138afd64e?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b3a3164fd0b28d429cd427aafae38a687a41a250a2bccf4ab3b0744138afd64e?s=96&d=identicon&r=g","caption":"Francesco de Vicariis"},"url":"https:\/\/blexin.com\/en\/author\/francesco-devicariisblexin-com\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/blexin.com\/wp-content\/uploads\/2020\/12\/e9e079d5-c58f-4ce0-b0d1-505d6fd97b57.png?fit=1024%2C608&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pcyUBx-7p6","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/28464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/users\/196716246"}],"replies":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/comments?post=28464"}],"version-history":[{"count":4,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/28464\/revisions"}],"predecessor-version":[{"id":31980,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/posts\/28464\/revisions\/31980"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/media\/28457"}],"wp:attachment":[{"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/media?parent=28464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/categories?post=28464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blexin.com\/en\/wp-json\/wp\/v2\/tags?post=28464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}