<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>distributed cache asp net &#8211; ASP.NET Hosting Reviews and Guides</title>
	<atom:link href="https://topreviewhostingasp.net/tag/distributed-cache-asp-net/feed/" rel="self" type="application/rss+xml" />
	<link>https://topreviewhostingasp.net</link>
	<description>ASP.NET Hosting &#124; Reviews &#124; Tips &#38; Tutorial</description>
	<lastBuildDate>Tue, 17 Jan 2023 04:28:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://topreviewhostingasp.net/wp-content/uploads/2017/01/cropped-trhaico-32x32.png</url>
	<title>distributed cache asp net &#8211; ASP.NET Hosting Reviews and Guides</title>
	<link>https://topreviewhostingasp.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Implement Distributed Cache in ASP.NET Core</title>
		<link>https://topreviewhostingasp.net/how-to-implement-distributed-cache-in-asp-net-core/</link>
					<comments>https://topreviewhostingasp.net/how-to-implement-distributed-cache-in-asp-net-core/#respond</comments>
		
		<dc:creator><![CDATA[Jacques Hunt]]></dc:creator>
		<pubDate>Tue, 17 Jan 2023 04:28:41 +0000</pubDate>
				<category><![CDATA[Hosting Tips]]></category>
		<category><![CDATA[asp net core cache]]></category>
		<category><![CDATA[asp net core tutorial]]></category>
		<category><![CDATA[asp net tips]]></category>
		<category><![CDATA[distributed cache asp net]]></category>
		<category><![CDATA[implement distributed cache]]></category>
		<guid isPermaLink="false">https://topreviewhostingasp.net/?p=3348</guid>

					<description><![CDATA[A distributed cache is a cache shared by multiple app servers, typically maintained as an external service to the app servers that access it. A distributed cache can improve the performance and scalability of an ASP.NET Core app, especially when the app is hosted by a cloud service or a server farm. This article will [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>A distributed cache is a cache shared by multiple app servers, typically maintained as an external service to the app servers that access it. A distributed cache can improve the performance and scalability of an ASP.NET Core app, especially when the app is hosted by a cloud service or a server farm.</p>



<p>This article will take you though some of the basic concepts of c# like Generic and asp.net core <strong>Dependency Injection</strong> and most importantly <strong>Distributed Caching</strong>, a reusable component. Caching is a very useful part of large applications, it minimizes the cost of making DB calls in API request. Here we will be implementing Distributed Caching. Let’s go ahead and get started.</p>



<h2 class="wp-block-heading">Steps by Steps to Implement Distributed Caching</h2>



<p><strong>1. Create new project</strong></p>



<p>Search for ASP.NET core web application and select it. Then click on Next</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="1024" height="549" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_1-1024x549.webp" alt="" class="wp-image-3349" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_1-1024x549.webp 1024w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_1-300x161.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_1-768x412.webp 768w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_1-50x27.webp 50w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_1.webp 1400w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Name the Project <strong>DistributedCaching</strong> and select all dropdown according to above selection. Then click on Create</figcaption></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="551" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_2-1024x551.webp" alt="" class="wp-image-3350" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_2-1024x551.webp 1024w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_2-300x162.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_2-768x414.webp 768w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_2-50x27.webp 50w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_2.webp 1400w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Choose API on next screen</figcaption></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="549" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_3-1024x549.webp" alt="" class="wp-image-3351" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_3-1024x549.webp 1024w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_3-300x161.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_3-768x412.webp 768w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_3-50x27.webp 50w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/VS_3.webp 1400w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div>


<p><strong>2. Package Installation</strong></p>



<p id="e6aa">Right-click on Project Solution select Manage Nuget Package and Install the following packages</p>



<p id="b1f7">· System.Data.SqlClient</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="720" height="93" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/sql.webp" alt="" class="wp-image-3352" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/sql.webp 720w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/sql-300x39.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/sql-50x6.webp 50w" sizes="(max-width: 720px) 100vw, 720px" /></figure></div>


<p><strong>3. Configuration</strong></p>



<p id="b321">After the project creation, go to <strong>Startup.cs. </strong>In the ConfigureServices method, we need to add Distributed Memory Cache service.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="720" height="144" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/configuration.webp" alt="" class="wp-image-3353" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/configuration.webp 720w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/configuration-300x60.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/configuration-50x10.webp 50w" sizes="(max-width: 720px) 100vw, 720px" /><figcaption>In appsettings.json, we need to write our SQL server data source, user, password, and database that will be used by our project.</figcaption></figure></div>


<pre class="wp-block-preformatted">"ConnectionStrings": {"SqlServerCString": "Data Source=localhost;Initial Catalog=caching;Persist Security Info=True;User ID=sa;Password=1234;"}</pre>



<p id="8a90">A complete view of appsettings.json</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="244" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/connectionstring-1024x244.webp" alt="" class="wp-image-3354" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/connectionstring-1024x244.webp 1024w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/connectionstring-300x71.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/connectionstring-768x183.webp 768w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/connectionstring-50x12.webp 50w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/connectionstring.webp 1108w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div>


<p><strong>4. Create Database</strong></p>



<ul><li>Open your SQL Server Management Studio and create a database named <strong>caching.</strong></li><li>Then open your query window and create a table named <strong>Student</strong> with the following query and insert some data using the following query</li></ul>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="661" height="192" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/sql-query.webp" alt="" class="wp-image-3355" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/sql-query.webp 661w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/sql-query-300x87.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/sql-query-50x15.webp 50w" sizes="(max-width: 661px) 100vw, 661px" /></figure></div>


<p><strong>5. Create Controller</strong></p>



<p id="dbdf">Click on the Controllers folder and add a new Controller. On the next screen select <strong>API Controller Empty </strong>and Click on <strong>Add</strong></p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="707" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/controller-1024x707.webp" alt="" class="wp-image-3356" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/controller-1024x707.webp 1024w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/controller-300x207.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/controller-768x531.webp 768w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/controller-50x35.webp 50w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/controller.webp 1174w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>On the next screen name the controller <strong>CachingController </strong>and click on Add</figcaption></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="709" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/cachingcontroller-1024x709.webp" alt="" class="wp-image-3357" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/cachingcontroller-1024x709.webp 1024w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/cachingcontroller-300x208.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/cachingcontroller-768x532.webp 768w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/cachingcontroller-50x35.webp 50w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/cachingcontroller.webp 1175w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Then open CachingController and do the following</figcaption></figure></div>


<pre class="wp-block-preformatted">[Route("api/[controller]")]</pre>



<p>to</p>



<pre class="wp-block-preformatted">[Route("api/caching")]</pre>



<p><strong>6. Create Database Model</strong></p>



<p id="dc87">Create a folder and name it Model, add Student.cs with the following code. Make Model serializable for further serializing in setting cache</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="206" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/database-model-1024x206.webp" alt="" class="wp-image-3358" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/database-model-1024x206.webp 1024w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/database-model-300x60.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/database-model-768x154.webp 768w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/database-model-50x10.webp 50w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/database-model.webp 1400w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div>


<p id="ad3f">Place the below code inside CachingController</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="865" height="795" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/database-model-2.webp" alt="" class="wp-image-3359" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/database-model-2.webp 865w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/database-model-2-300x276.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/database-model-2-768x706.webp 768w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/database-model-2-50x46.webp 50w" sizes="(max-width: 865px) 100vw, 865px" /><figcaption>After placing code, make a request from <strong>POSTMAN </strong>to <a href="https://localhost:%3cPORT%3e/api/caching" target="_blank" rel="noreferrer noopener">https://localhost:&lt;PORT>/api/caching</a> and you will get back the following response. You can notice response time <strong>1348 MS</strong></figcaption></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="443" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/code-1024x443.webp" alt="" class="wp-image-3360" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/code-1024x443.webp 1024w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/code-300x130.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/code-768x332.webp 768w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/code-50x22.webp 50w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/code.webp 1265w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>Create a folder Service and add MemoryCache.cs class and IMemoryCache.cs with the following code.</figcaption></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="287" height="288" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/distributecache.webp" alt="" class="wp-image-3361" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/distributecache.webp 287w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/distributecache-150x150.webp 150w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/distributecache-50x50.webp 50w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/distributecache-70x70.webp 70w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/distributecache-127x127.webp 127w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/distributecache-125x125.webp 125w" sizes="(max-width: 287px) 100vw, 287px" /></figure></div>


<p id="88ae">IMemoryCache.cs</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="513" height="171" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/imemorycache.webp" alt="" class="wp-image-3362" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/imemorycache.webp 513w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/imemorycache-300x100.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/imemorycache-50x17.webp 50w" sizes="(max-width: 513px) 100vw, 513px" /></figure></div>


<p id="539e">MemoryCache.cs</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="720" height="735" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache.webp" alt="" class="wp-image-3363" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache.webp 720w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache-294x300.webp 294w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache-50x50.webp 50w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache-70x70.webp 70w" sizes="(max-width: 720px) 100vw, 720px" /></figure></div>

<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="720" height="648" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_2.webp" alt="" class="wp-image-3364" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_2.webp 720w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_2-300x270.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_2-50x45.webp 50w" sizes="(max-width: 720px) 100vw, 720px" /></figure></div>


<p id="773b">After placing all code accordingly, we will do <strong>Dependency Injection</strong> MemoryCache in <strong>Startup.cs</strong>. Add the following code inside the <strong>ConfigureServices</strong> method</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="513" height="171" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_3.webp" alt="" class="wp-image-3365" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_3.webp 513w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_3-300x100.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_3-50x17.webp 50w" sizes="(max-width: 513px) 100vw, 513px" /></figure></div>


<p id="f4ea">Now&nbsp;<strong>MemoryCache&nbsp;</strong>implementation available throughout Application Lifetime and an instance will be created every time there will be a request to the server.</p>



<p id="d1a3">Now let’s go for caching the result after fetching data from Student Table</p>



<ul><li>First, let’s receive MemoryCache through Constructor Injection in CachingController.cs with the following code.</li></ul>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="702" height="169" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_4.webp" alt="" class="wp-image-3366" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_4.webp 702w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_4-300x72.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_4-50x12.webp 50w" sizes="(max-width: 702px) 100vw, 702px" /></figure></div>


<ul><li>Let’s cache student list after fetching data from Student Table, update the GetResult method in CachingController with the following code</li></ul>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="662" height="656" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_5.webp" alt="" class="wp-image-3367" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_5.webp 662w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_5-300x297.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_5-150x150.webp 150w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_5-50x50.webp 50w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_5-70x70.webp 70w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_5-127x127.webp 127w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_5-125x125.webp 125w" sizes="(max-width: 662px) 100vw, 662px" /></figure></div>


<p>So now fetched data from DB is now stored in a cache, now we have to check if student data available in the cache then fetch data from the cache otherwise make DB call and store it in the cache using the same method. Update the GetResult method with the following code.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="720" height="805" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_6.webp" alt="" class="wp-image-3368" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_6.webp 720w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_6-268x300.webp 268w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/memorycache_6-45x50.webp 45w" sizes="(max-width: 720px) 100vw, 720px" /></figure></div>


<p>So now make a request from POSTMAN to <a href="https://localhost:%3cPORT%3e/api/caching" target="_blank" rel="noreferrer noopener">https://localhost:&lt;PORT>/api/caching</a> and see the difference in response time</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="448" src="https://topreviewhostingasp.net/wp-content/uploads/2023/01/1-1024x448.webp" alt="" class="wp-image-3369" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/01/1-1024x448.webp 1024w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/1-300x131.webp 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/1-768x336.webp 768w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/1-50x22.webp 50w, https://topreviewhostingasp.net/wp-content/uploads/2023/01/1.webp 1267w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div>


<p id="f4fc">You can see time&nbsp;<strong>7 MS</strong>, this makes a huge difference in server efficiency using the caching method.</p>



<p id="3402">Now, we have created a service that will be injected in Startup.cs file, a component that can be used in any project for caching. One more point if you want to remove a particular cache from memory then in the same way call <strong>removeCache</strong> and pass the <strong>CacheKeys</strong> inside the function. It will remove cache from memory.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://topreviewhostingasp.net/how-to-implement-distributed-cache-in-asp-net-core/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
