NCache 4.3 - Online Documentation

Using NCache in Windows Azure

NCache distributed cache in Windows Azure VMs is utilized in the client applications using NCache remote client libraries and here are two possible
application deployments.
NCache Remote Clients: These are your applications running on different VM than the cache cluster.
Virtual Machines (VMs) for .NET: You can choose to deploy your .NET applications directly on VMs. In case of .NET applications, you have the option of
either deploying NCache client libraries with the application or installing NCache remote client on this Virtual Machine through a Windows Installer
.msi file.
NCache Equipped Web Roles or Worker Roles for .NET: You can choose to deploy your .NET application as a Web Role or Worker Role. NCache client
libraries are deployed along with your application. NCache provides an InProc or OutProc client cache even in this environment.
Virtual Machines (VMs) for .NET
This is regular client deployment where users can log into Azure VMs and then install NCache remote client or deploy client libraries directly. You can
then use NCache for applications that are going to be deployed on this virtual machine. There are no special configurations involved for Azure in this
NCache Equipped Web Roles for .NET
You can choose to deploy your .NET application as a Web Role or Worker Role. NCache client libraries are deployed along with your application.
Here is step by step guide on how to use NCache in Windows azure project using an ASP.NET Web role.
Step1: Create a Windows Azure Project: Create a new project and choose Cloud from installed templates and then name it.
              Choose ASP.NET Web Role on next screen and then choose OK.
            This will create a Windows Azure cloud project with an ASP.NET web role.
Step2: Configure Network Settings of Web Role: For single site deployment where cache servers and clients are on same virtual
network then add web role network settings within the “ServiceConfiguration.Cloud.cscfg” file.
These settings will dictate that web role is also deployed on the same virtual network and subnet where NCache servers are residing.
    <VirtualNetworkSite name="NCacheVirtualNetwork"/>
      <InstanceAddress roleName="NC-SessionAndObjects">
          <Subnet name="NCacheSubnet" />
Step3: Equip Web role with NCache assembly and config files: Now, the Web role needs to be equipped so that it can be used by NCache. This requires NCache assembly reference, config files and code/configuration changes. You will make necessary configurations and code changes to use NCache for session and object caching later on.
Here are two options for this.
Using NCache Azure NuGet Package:
Manually Configure NCache with Web Role:
Using NCache Azure NuGet Package to configure NCache with a Web Role:
NCache provides an Azure NuGet package that can be used to automatically configure your web role for NCache. This will automatically include
references of all NCache and dependant assemblies along with the NCache client configurations files. You can then change NCache configurations as
per your requirements.
Here are steps:
Step1: Install NCache Azure NuGet Package: Download the NCache Azure NuGet project directly from our website. You then need to add
it in visual studio using NuGet Package manager and install it in the project. This will automatically include all required NCache assemblies
and configuration files in the Azure project.
Step2: Verify NCache Assemblies: : Verify following assembly references in project references as soon as NCache Azure NuGet package is installed
within the project. It will also include following dependant assemblies (Log4Net.dll, Protobuf-net.dll and Oracle.DataAccess.dll).
Step3: Verify NCache Configs: Verify that Config folder containing the NCache config files (Client.ncconf and Config.ncconf files) is added in the project.
Step4: Refer to ReadMe file: It also includes a “Readme.txt” file that explains Web.config changes for NCache session Store Provider and also
NCache Client.ncconf changes to connect to an already configured cache in environment. This information can be used as reference later
when you will change NCache configurations.
Manually Configure Web Role for NCache:
If you do not want to use NCache Azure NuGet package then you need to manually add all NCache and its dependent assemblies along with
required NCache configuration files.
Here is step by step detail on this.
Step1: Add NCache remote client assembly references: Add all NCache client assembly references within your Windows Azure project. These
assemblies can be downloaded from our website separately.
Now right click on the assemblies, select properties and set Copy Local to “True” for all NCache Assemblies
Step2: Include Client.ncconf file in the Web role: Add NCache config files (Config.ncconf and Client.ncconf) manually within the Windows Azure
project. These come installed with NCache installation and can be copied from any box.
Right-click on the “Client.ncconf” and “Config.ncconf” file and select Properties. Set the Copy to Output Directory to Copy Always
Step5: Modify NCache Client.ncconf file to connect to a cache:
Client.ncconf file is used to connect to any clustered cache. It contains server IP addresses where clustered cache resides. To connect to the already configured cache on Azure VMs, it is important to modify the client.ncconf file.
Here is how to update this file.
For single site deployment: If the Web Role is going to be deployed in the same Virtual Network as the Virtual Machines then there are no special
modifications involved.
Here is a sample cache tag for cache named “Democache” and is configured on Azure VMs with IP addresses and
<cache id="Democache" client-cache-id="" client-cache-syncmode="optimistic" default-readthru-provider="" default-writethru-provider=""
<server name=""/>
<server name=""/>
Web role should be able to connect to the cache without any explicit port forwarding here.
For Multi Site deployment: If Web Role is going to be deployed outside the Virtual Network of the cache servers, then we need to provide an
additional server IP mapping tag in Client.ncconf file. This information will be used to connect to our cache from outside its virtual network. Here is a sample cache tag to connect outside its virtual network.
<cache id="AzureCache" client-cache-id="" client-cache-syncmode="optimistic" default-readthru-provider="" default-writethru-provider=""
<server name=""/>
<server name=""/>
<mapping public-ip="" public-port="9800" private-ip="" private-port="9800"/>
<mapping public-ip="" public-port="9801" private-ip="" private-port="9800"/>
</server-ip-mapping >
Following are details on the specified public and private IP addresses and ports.
Public IP Address of the virtual machine is where a clustered cache is configured.
Public port of the Virtual Machine starts with 9800 and increment by one for each cache server. This is public End point defined for our respective
Cache VM.
Private IP is the internal or actual IP Address of each Virtual Machine.
Private Port is the port of NCache which is 9800 and is same for all NCache VMs and is defined as private end point above.
Step6: Use NCache for objects and Sessions caching: In order to use NCache for object caching where you'll be making NCache API calls,
then do the following in your Visual Studio web role project C# or VB.NET source code.
Step1: Add NCache namespaces: It is required to include following namespaces in the source code to use NCache client API.
using Alachisoft.NCache.Web.Caching;
using Alachisoft.NCache.Runtime;
Step2: Include NCache API to test the cache: Add following code to quickly test the cache
  _cache = NCache.InitializeCache("DemoCache");
            //Add items in the cache
            for (int i=0; i<100;i++)
                _cache.Insert("Key" + i.ToString(), "DummyObject" + i.ToString());
Step3: Use NCache for Session caching: If using NCache for ASP.NET Sessions, you need to modify your "web.config". You need to add NCache Session State Provider (SSP) configurations in "web.config" as followed:
<compilation defaultLanguage ="C#debug="true>
<add assembly="Alachisoft.NCache.SessionStoreProvider,
Please make sure that "Version" above reflects the correct version of NCache.
<sessionState cookieless = "falseregenerateExpiredSessionId = "truemode = "CustomcustomProvider = "NCacheSessionProvidertimeout= "20">
<add name = "NCacheSessionProvidertype = "Alachisoft.NCache.Web.SessionState.NSessionStoreProvidersessionAppId ="NCacheTest"
 cacheName = "myTestCachewriteExceptionsToEventLog = "false"/>
Step7: Test Web role locally or Deploy in Azure environment: Visual studio enables users to emulate cloud environment and it can be tested locally or it can be deployed in Azure environment and tested there. You can test the web role once you have completed above steps and verify that it works.
See Also

© Copyright  2014 Alachisoft. All rights reserved.