<?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>NCache - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/category/ncache/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Sun, 26 Nov 2023 04:37:59 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>NCache - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Distributed Cache Using NCache In .NET with Examples</title>
		<link>https://thecodebuzz.com/ncache-distributed-cache-using-ncache-dotnet-core-examples/</link>
					<comments>https://thecodebuzz.com/ncache-distributed-cache-using-ncache-dotnet-core-examples/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 15 May 2022 23:33:00 +0000</pubDate>
				<category><![CDATA[NCache]]></category>
		<category><![CDATA[Distributed Cache Using NCache]]></category>
		<category><![CDATA[is ncache free]]></category>
		<category><![CDATA[ncache edition comparison]]></category>
		<category><![CDATA[ncache github]]></category>
		<category><![CDATA[ncache net core example]]></category>
		<category><![CDATA[ncache open source limitations]]></category>
		<category><![CDATA[ncache pricing]]></category>
		<category><![CDATA[ncache tutorial c#]]></category>
		<category><![CDATA[ncache vs redis]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=22167</guid>

					<description><![CDATA[<p>NCache Distributed Cache in C#.NET with Examples Today in this article, we will see how to use distributed Cache Using NCache in C#.NET with examples. We will also see how to leverage the IDistributedCache interface for performing cache operations using NCache. Today in this article, we will cover below aspects, NCache is a distributed in-memory [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/ncache-distributed-cache-using-ncache-dotnet-core-examples/">Distributed Cache Using NCache In .NET with Examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>NCache Distributed Cache in C#.NET with Examples</strong></h1>



<figure class="wp-block-image size-large is-resized"><a href="https://www.thecodebuzz.com/getting-started-with-ncache-in-asp-net-core/" target="_blank" rel="noopener"><img fetchpriority="high" decoding="async" width="1024" height="639" src="https://www.thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-using-IDistributedCache-1024x639.jpg" alt="Distributed Cache Using NCache,AddNCacheDistributedCache " class="wp-image-22188" srcset="https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-using-IDistributedCache-1024x639.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-using-IDistributedCache-300x187.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-using-IDistributedCache-768x479.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-using-IDistributedCache-1536x958.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-using-IDistributedCache-785x490.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-using-IDistributedCache.jpg 1774w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>Today in this article, we will see how to use distributed Cache Using NCache in C#.NET with examples. We will also see how to leverage the <strong><em>IDistributedCache </em></strong>interface for performing cache operations using NCache.</p>



<p></p>



<p>Today in this article, we will cover below aspects,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-using-idistributedcache-for-ncache">Using IDistributedCache for NCache</a></li><li><a href="#aioseo-benefits-of-ncache">Benefits of NCache</a></li><li><a href="#aioseo-getting-started">Getting started</a></li><li><a href="#aioseo-create-asp-net-core-api-mvc-application">Create ASP.NET Core API/ MVC Application</a></li><li><a href="#aioseo-using-addncachedistributedcache-in-the-api-pipeline">Using AddNCacheDistributedCache in the API pipeline</a></li><li><a href="#aioseo-idistributedcache-using-constructor-injection">IDistributedCache using Constructor injection</a></li></ul></div>



<p></p>



<p>NCache is a distributed in-memory database. </p>



<p></p>



<p><strong>NCache</strong> is one of the Open Source in-memory, distributed caches for .Net Core which is extremely scalable, and caches application data quickly.</p>



<p></p>



<p></p>



<p>In our previous article, we looked at how to leverage the <em><strong>ICache </strong></em>interface which is provided by <em><strong>NCache</strong></em>. For more details please visit the below article,</p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/redis-distributed-cache-asp-net-core-csharp-redis-examples/" target="_blank" rel="noreferrer noopener" title="Redis Distributed cache in .NET with examples"><strong><em>NCache -Distributed Cache using ICache interface in .NET</em></strong></a></li>
</ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-using-idistributedcache-for-ncache"><em>Using IDistributedCache for NCache</em></h2>



<p></p>



<p>We can leverage a Microsoft-provided <em><strong>IDistributableCache</strong></em> interface to leverage the Cache capability in the applications. </p>



<p></p>



<p>This interface comes as caching abstraction extension which can be used and plugged into any cache of your choice like <strong><em>Redis</em></strong> or <strong><em>NCache </em></strong>solutions etc.</p>



<p></p>



<p>This Interface exposes the below methods for performing get or update or deletes operations on the cache,</p>



<p></p>



<ul class="wp-block-list">
<li><strong>Get and GetAsync</strong></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>Set and SetAsync</strong></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>Refresh and RefreshAsync</strong></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>Remove and RemoveAsync</strong></li>
</ul>



<p></p>



<p>In today&#8217;s article, however, we will cover, how to use the <strong><em>ICache </em></strong>interface which is very rich and provides a lot of capabilities compare to the <em><strong>IDistributableCache </strong></em>interface.</p>



<p></p>



<p>Let&#8217;s brief about on the few benefits of NCache, which we learned in the previous article,</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-benefits-of-ncache">Benefits of NCache</h2>



<p></p>



<ul class="wp-block-list">
<li>Using <strong>NCache </strong>lets you access the data more quickly to lower the response time from an external system.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>We can utilize  <strong>NCache</strong> to scale Applications by removing performance constraints associated with data storage and databases.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>NCache</strong> is an efficient way of getting the data, especially frequently needed information and their access.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>NCache</strong> can be used HttpBasedCaching, In-Memory Caching, or Distributed Caching.</li>
</ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-getting-started">Getting started </h2>



<p></p>



<p>Before we get started, please make sure you have the NCache cache server setup configured. For this demonstration, I am using local demoCache as a Cache.</p>



<p></p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="421" src="https://www.thecodebuzz.com/wp-content/uploads/2022/05/NCache-.Net-C-example-1-1024x421.jpg" alt="NCache C# ASP.NET Core" class="wp-image-22124" srcset="https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-.Net-C-example-1-1024x421.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-.Net-C-example-1-300x123.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-.Net-C-example-1-768x316.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-.Net-C-example-1-1536x632.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-.Net-C-example-1-2048x843.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-.Net-C-example-1-785x323.jpg 785w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p><strong>Prerequisites:</strong></p>



<p></p>



<ul class="wp-block-list">
<li>Please make sure NCache is installed and configured correctly on the server. </li>
</ul>



<ul class="wp-block-list">
<li>Please make sure to Configure the server/cluster in<strong><em> <a href="https://www.alachisoft.com/resources/docs/ncache/admin-guide/client-config.html" target="_blank" rel="noopener nofollow" title="">client.ncconf.</a></em></strong></li>
</ul>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>When you install the NCache Nuget package, it adds the<strong><em> Client.ncconf and  Config.ncconf </em></strong> files to you local application.</p>



<p><br>&#8211;<strong><em>Client.ncconf</em></strong> contains the information about the cache servers.</p>



<p></p>



<p>&#8211;<strong><em>Config.ncconf </em></strong>that is copied locally is used for <strong><em>local inproc cache and inproc client caches</em></strong>.Also config.ncconf must also contain the name for the clustered cache which this client cache is a part of the applicaiton</p>
</blockquote>



<p></p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-create-asp-net-core-api-mvc-application">Create ASP.NET Core API/ MVC Application</h2>



<p></p>



<p>Create .NET <strong><em>3.1 or 5 API</em></strong> or ASP.NET Core MVC applications. </p>



<p></p>



<p><strong>Set Up Environment for AspNetCore.Session.NCache</strong></p>



<p></p>



<p>Please install the NCache NuGet package version as required,</p>



<p></p>



<p><strong>For OpenSource:</strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">Install-Package NCache.Microsoft.Extensions.Caching.OpenSource</pre>



<p></p>



<p>For the<strong> Enterprise</strong>/<strong>Professional</strong>  version, please install the below version as required,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">Install-Package NCache.Microsoft.Extensions.Caching</pre>



<p></p>



<p>OR</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">Install-Package NCache.Microsoft.Extensions.Caching.Professional</pre>



<p></p>



<h2 class="wp-block-heading" id="aioseo-using-addncachedistributedcache-in-the-api-pipeline">Using <em>AddNCacheDistributedCache </em>in the API pipeline</h2>



<p></p>



<p></p>



<p>Update the <strong><em>ConfigureServices </em></strong>methods with the below-highlighted code,</p>



<p></p>



<pre class="wp-block-code"><code>public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddScoped&lt;IEmployeeRepository, EmployeeRepository>();
            services.AddScoped&lt;INCacheCacheService, NCacheCacheService>();
            services.AddDbContext&lt;EmployeeContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("EmployeeDB"),
                sqlServerOptionsAction: sqlOptions =>
                {
                    sqlOptions.EnableRetryOnFailure();
                });
            });
            
            //Lets you Use NCache as Distributed cache in the applicaiton
            services.AddNCacheDistributedCache(configuration =>
             {
                 configuration.CacheName = Configuration&#91;"NCacheConfig:CacheName"];
             });
        }</code></pre>



<p></p>



<p>We will see implementation details for <strong><em>INCacheCacheService </em></strong>and <em><strong>IEmployeeRepository </strong></em>below.</p>



<p></p>



<p><strong><em>INCacheCacheService </em></strong>is defined as below,</p>



<p></p>



<pre class="wp-block-code"><code>public interface INCacheCacheService
    {
        T Get&lt;T>(string key);
        T Set&lt;T>(string key, T value);
    }</code></pre>



<p></p>



<h2 class="wp-block-heading" id="aioseo-idistributedcache-using-constructor-injection"><strong>IDistributedCache using Constructor injection </strong></h2>



<p></p>



<p>Below we are injecting the <strong><em>IDistributedCache</em></strong> interface through constructor injection.</p>



<p></p>



<p>Here is a sample implementation <strong><em>INCacheCacheService </em></strong>interface using <strong><em>IDistributedCache</em></strong>.</p>



<p></p>



<pre class="wp-block-code"><code>public class NCacheCacheService : INCacheCacheService
    {
        private readonly IDistributedCache _cache;
        public NCacheCacheService(IDistributedCache cache)
        {
            _cache = cache;
        }
        public T Get&lt;T>(string key)
        {
            var value = _cache.GetString(key);
            if (value != null)
            {
                return JsonSerializer.Deserialize&lt;T>(value);
            }
            return default;
        }
        public T Set&lt;T>(string key, T value)
        {
            _cache.SetString(key, JsonSerializer.Serialize(value));
            return value;
        }
..
}</code></pre>



<p></p>



<p>The following sample code snippet is getting the Employee detail based on the cache key, where </p>



<p></p>



<ul class="wp-block-list">
<li>If an item exists in the cache, the item will be returned straight from the cache, avoiding the heavy call to the database.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>If the item doesn&#8217;t exist in the cache, the item will be fetched from the actual database the very first time.
<ul class="wp-block-list">
<li>Also, at the same time cache is updated with that fetched item. </li>



<li>For any subsequent calls on the same key/item, data will be provided from NCache.</li>
</ul>
</li>
</ul>



<p></p>



<p>Below is a sample implementation for the <strong><em>Repository </em></strong>class. </p>



<p></p>



<pre class="wp-block-code"><code>public class EmployeeRepository : IEmployeeRepository
    {
        private readonly EmployeeContext _context;
        private readonly INCacheCacheService _nCache;
        public EmployeeRepository(EmployeeContext context, INCacheCacheService nCache)
        {
            _context = context;
            _nCache = nCache;
        }
        public Employee GetEmployee(int? employeeID)
        {
            var cachedId = _nCache.Get&lt;Employee>(employeeID.ToString());
            if (cachedId != null) return cachedId;//return cache saved ids
            else
            {   
                //If Cache doesnt exist then get the data from actual source like database
                //service or external vendor sevrvices etc.
                Employee emp = _context.Employee.Find(employeeID.ToString());
                //Lets now cache this object
                if(emp!=null)
                _nCache.Set&lt;Employee>(employeeID.ToString(), emp);
                //return the same object if accessed first time within timeout period
                return emp;
            }
        }
..
..
}</code></pre>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="777" src="https://www.thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-C.NET-with-Examples-using-IDistributedCache-1024x777.jpg" alt="" class="wp-image-22186" srcset="https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-C.NET-with-Examples-using-IDistributedCache-1024x777.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-C.NET-with-Examples-using-IDistributedCache-300x228.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-C.NET-with-Examples-using-IDistributedCache-768x583.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-C.NET-with-Examples-using-IDistributedCache-1536x1166.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-C.NET-with-Examples-using-IDistributedCache-685x520.jpg 685w, https://thecodebuzz.com/wp-content/uploads/2022/05/NCache-Distributed-Cache-in-C.NET-with-Examples-using-IDistributedCache.jpg 1581w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>If you are interested to leverage the <em><strong>ICache </strong></em>interface from <em><strong>NCache </strong></em>using dependency injection, then please visit <a href="https://www.thecodebuzz.com/getting-started-with-ncache-in-asp-net-core/" title="Getting started with NCache In ASP.NET Core" target="_blank" rel="noreferrer noopener"><em><strong>this </strong></em></a>article for more details.</p>



<p></p>



<p><strong><em>Useful References: </em></strong></p>



<p></p>



<ul class="has-medium-font-size wp-block-list">
<li><a href="https://www.thecodebuzz.com/what-is-cache-best-practices-guidelines/" target="_blank" rel="noreferrer noopener" title="What is Cache – Guidelines and Best Practices"><strong><em>What is Cache – Guidelines and Best Practices</em></strong></a></li>
</ul>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/ncache-distributed-cache-using-ncache-dotnet-core-examples/">Distributed Cache Using NCache In .NET with Examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/ncache-distributed-cache-using-ncache-dotnet-core-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
