Use in ASP.NET Core

Hi everyone, I'm Sean, and today I'm going to show you how you can get started with Using NCache and ASP.NET Core. It's actually a very simple process that we will achieve through a series of very basic steps. So, without further ado, let's jump straight into it.

NCache Deployment Overview

Starting with what a typical deployment of NCache would look like. This represents what a typical deployment would look like. With your application tier on the top, your caching tier in the middle, and your database tier at the bottom. This is also found as a shared deployment where you can have your caches hosted on your app servers along with your applications but this is the more recommended approach as it makes for a cleaner architecture.

NCache Deployment
NCache Deployment

Environment Setup

So, now that we have the deployment understood let's take a look at what it takes to integrate NCache starting with the environment setup. So, for an environment setup I have my two NCache servers with my caches configured and ready to go, and I'm going to show you that right now. So, first let me show you this application that we're going to be working with. This is our sample application my customer app that you can see here, and if I open up my web manager you can see my two caches configured here, my data cache and my reference cache that are hosted on 105 and 106. So, these are two caches that I have configured for this demo. They're ready to go. So, we can actually move on to the actual steps.

Demo Caches
  1. Install the NCache SDK NuGet Package

    The first step being to install the NCache SDK NuGet Package within the application. Coming back to the application if I manage the NuGet Packages we will find that I have the latest version of the NCache SDK already installed, so, I'm good to go here.

  2. Add the NCache Namespaces

    So, we can move on to the next step which is to add the NCache namespaces in the application. Coming back, I'm going to head over to my customer repository, and over here I simply have to go ahead and add using Alachisoft.NCache.Client. And, we can see that once I've added this namespace the red squiglies are gone which means that we needed this namespace, we're good to go. I'm just going to save that.

  3. Add the NCache Configuration

    And, now the third step is to add the NCache configuration to the .ncconf files. Coming back to the application you will find that I have this client and config.ncconf file within my application. You might be wondering at this point where did we actually get these files. Well, these files are brought in with the NCache SDK. So, when you install the NCache SDK or add that NuGet Package these files are brought into your application. So, now that we know where they came from let's go ahead and take a look at my client.ncconf. So, opening that up we can see that I have the cache tags for the ‘datacache’ and the ‘referencecache’ hosted on 105 and 106 respectively. 105 and 106, there we go. And, if I head over to config we can see that I have my ‘refclientcache’ configured that is speaking to my reference cache. And, this is my InProc client cache. Now, I could go into detail regarding the benefits and features that the NCache client cache provides but there's a whole dedicated video on this that I do recommend that you go and check out.

    <?xml version="1.0" encoding="UTF-8"?><configuration>
        <ncache-server connection-retries="3" retry-connection-delay="0" retry-interval="1" command-retries="3" command-retry-interval="0.1" client-request-timeout="90" connection-timeout="5" port="9800"/>
    	  <cache id="data" client-cache-id="" client-cache-syncmode="optimistic" skip-client-cache-if-unavailable="True" reconnect-client-cache-interval="10" default-readthru-provider="" default-writethru-provider="" load-balance="False" enable-client-logs="False" log-level="error">
    		  <server name="20.200.16.105"/>
    		  <server name="20.200.16.106"/>
    	  </cache>
    	  <cache id="reference" client-cache-id="refclientcache" client-cache-syncmode="optimistic" skip-client-cache-if-unavailable="True" reconnect-client-cache-interval="10" default-readthru-provider="" default-writethru-provider="" load-balance="True" enable-client-logs="False" log-level="error">
    		  <server name="20.200.16.105"/>
    		  <server name="20.200.16.106"/>
    	  </cache>
    </configuration>
  4. Implement Basic Operations into Existing Methods

    Anyway, so now that we've done this, let's go ahead and move on to the next step which is to implement our basic operations into our existing methods. So, I have my customer repository class here, and I have my caching methods defined here.

    //Constructor With Caching
    public CustomerRepository(string connectionString, string data_cache, string ref_cache) {
        _connectionString = connectionString;
        if (_data_cache == null) {
            _data_cache = CacheManager.GetCache(data_cache);
            Console.WriteLine($ "Connected to {data_cache}");
        }
        if (ref_cache == null) {
            _ref_cache = CacheManager.GetCache(ref_cache);
            Console.WriteLine($ "Connected to {ref_cache}");
        }
    }

    So, if we're going to start with the actual fetching of the cache handles which we're going to do using this CacheManager.GetCache for my data_cache and my _ref_cache. So, you can see that we're connecting to both the data and the ref cache in this application.

    Next we're getting our customers by ID for which we're using this _data_cache.Get<Customer and we're passing the string of the customer ID or the key that's going to give us the customer. And, we're going to fetch it from the _ref_cache if we don't get it from the customer cache. So, the same line for the _ref_cache beneath that.

    public Customer GetCustomerById(string id) {
        Customer customer = null;
        if (_data_cache != null) {
            customer = _data_cache.Get <Customer> ($ "Customer:{id}");
            if (customer != null) {
                return customer;
            }
        }
        if (_ref_cache != null) {
            customer = _ref_cache.Get <Customer> ($ "Customer:{id}");
            if (customer != null) {
                return customer;
            }
        }
        //Fetch customer from DB
    	...
    
        //Add customer to reference cache
        _ref_cache.Add($ "Customer:{customer.CustomerID}", customer);
        customerKeys.Add($ "Customer:{customer.CustomerID}");
        return customer;
    }

    And, now the next method actually is to add our customer to the cache for which we're going to be adding it to our _data_cache, so, we say _data_cache.Add this customer key and this customer which is going to add that key value pair then into our cache.

    if (_data_cache != null) {
        if (_data_cache.Get <Customer> ($ "Customer:{customer.CustomerID}") == null) {
            Console.WriteLine($ "Adding customer {customer.CustomerID} in data cache");
            _data_cache.Add($ "Customer:{customer.CustomerID}", customer);
            customerKeys.Add($ "Customer:{customer.CustomerID}");
        } else {
            Console.WriteLine($ "Customer {customer.CustomerID} already exists in data cache.");
        }
    }

    And, in the same way I've defined all the other methods to work. So, this is good here.

  5. Deploy the App

    The last step is to actually deploy our application for which we're going to do this in two ways.

    1. Deployment using NuGet

      The first way we're going to be deploying this application is to be deploying it using the NuGet Package only which is how we've configured it so far. Let's take a look at how we're going to do this.

      So, I have a web server configured with this application already deployed there and that is my 104 box. So, going to my 104 box you can see that I have my NuGet site here. I'm just going to go ahead and browse this or explore this and we can see that I have my client.ncconf. Opening this file shows us that I have the ‘datacache’ and the ‘referencecache’ tags both pointing to 105 and 106 respectively. So, this looks correct.

      Heading over to the config.ncconf we can see my ‘refclientcache’ here that is pointing to my ‘ReferenceCache’ which means that even this looks good. So, with all these looking good this app is already deployed. Let's go ahead and take a look at this app now. So, going to 104 like. So, you can see I have this NuGet Only CustomerApp deployed. Now, let's add a customer. So, adding a customer here now you can see that our customers have gone up to 92. So, we've done something in this application.

      Now, let's take a look at what our clustered caches are showing us against this application. So, I'm also going to open up the reference cache monitor here, and we should see 91 customers in our reference cache since that was the number of customers when we first load it up, and indeed we see 91, 40 + 51 so that's 91. And, in the data cache we should see 1 customer which in fact we do, and this is the customer we just added.

      NCache Monitor

      Now, let's go ahead and get rid of this customer. So, I'm just going to look for this customer and I'm going to delete it so. There we go, we've deleted it. So, going back to the data cache now with 91 customers we see that the customer count has gone from 1 to zero. So, this application is working and this is one way to deploy this application.

    2. Deployment using Remote Client

      So, now the other way to deploy this application is to be deploying this application using the NCache Remote Client. Let's take a look at how this is going to work. So, I have another box set up for this that would be my 103 box. Let me take you there now, and I have a ConfigBased version of my application stored here. I named it config based because we're going to be actually using a configuration from the NCache management that we're going to give to this box. So, as you can see I have no client or config.ncconf files within my application. So, that means that this app shouldn't work, right? Well, actually if I run a Get-NCacheVersion in the PowerShell you can see that I have the Enterprise Remote Client installed on this machine.

      So, let's go ahead and add this machine as a client node to our clustered caches using the NCache Management Center. So, coming back to my manager I'm going to click on view details for my reference cache and if I scroll down we can see that 103 is already added with a client cache configured that being the ReferenceClientCache, and it is an OutProc client cache which is something I can configure when I have the NCache Remote Client. Going into my data cache we can see that 103 is configured as a client as well.

      NCache Remot Client Configuration

      So, going back to 103 we can go into the NCache installation directory and see these configurations in our config files. So, if I go into program files > NCache > config and client.ncconf we can see that I have my datacache tag here and my referencecache tag here. In my config.ncconf we can see the reference client cache tag pointing to the reference cache. So, everything looks good here.

      <?xml version="1.0" encoding="UTF-8"?>
        <configuration>
          <ncache-server connection-retries="3" retry-connection-delay="0" retry-interval="1" command-retries="3" command-retry-interval="0.1" client-request-timeout="90" connection-timeout="5" port="9800"/>
      	  <cache id="data" client-cache-id="" client-cache-syncmode="optimistic" skip-client-cache-if-unavailable="True" reconnect-client-cache-interval="10" default-readthru-provider="" default-writethru-provider="" load-balance="False" enable-client-logs="False" log-level="error">
      		  <server name="20.200.16.105"/>
      		  <server name="20.200.16.106"/>
      	  </cache>
      	  <cache id="reference" client-cache-id="refclientcache" client-cache-syncmode="optimistic" skip-client-cache-if-unavailable="True" reconnect-client-cache-interval="10" default-readthru-provider="" default-writethru-provider="" load-balance="True" enable-client-logs="False" log-level="error">
      		  <server name="20.200.16.105"/>
      		  <server name="20.200.16.106"/>
      	  </cache>
      	  <cache id="refclientcache" client-cache-id="" client-cache-syncmode="optimistic" skip-client-cache-if-unavailable="True" reconnect-client-cache-interval="10" default-readthru-provider="" default-writethru-provider="" load-balance="True" enable-client-logs="False" log-level="error">
      		  <server name="20.200.16.103"/>
      	  </cache>
        </configuration>

      And, I already have this application deployed, so, now I'm just going to close this here and let's actually open this website up in our browser. So, in our browser I have it open here in this tab we can see 103 at the top and these 91 customers here. So, this app is already working now let's go ahead and add a customer from here. So, adding that customer again let's go back to our data cache, and there we go. You can see that that customer has been added into the cache again and we see activity there. And, this is the other way that you can deploy this application.

And, that pretty much sums up the two ways in which you can work and use NCache in ASP.NET Core and integrated it into your existing ASP.NET Core applications.

Next Steps

If you want to try this out yourself you can download the free 30-day trial of NCache Enterprise and Professional from our website which will give you a fully working product to try out. And, this lets you play around with the complete feature set of NCache with no restrictions. Let me take you there now. So, if I go to alachisoft.com, and I go to the download page you can see that you have these options to download NCache from here. You can also try the NCache Playground which gives you sandbox sessions and predefined samples to play around with and you can do that by clicking on this try playground button here. Lastly you can schedule a personalized NCache demo where we will give you a 1-hour technical dump to map NCache to your use case. You can do that from Resources > Schedule a Demo and then you have this form here that you just fill out and you send to us and we're going to schedule that demo for you right away.

And, that pretty much brings me to the end of this demonstration. Thank you so much for watching and I do hope that you can get your applications your ASP.NET Core applications working with NCache after watching this video. Thank you so, much for watching, take care now, bye-bye.

What to Do Next?

© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.