NCache 4.3 - Online Documentation

Using Client cache in Windows Azure

NCache provides support for client cache in windows Azure for applications deployed in web role/worker role. Client cache can be either In-proc or Out-proc, it improves performance of your application for reference data by keeping data closer to your applications and saves trips across the network to clustered cache.
Please see more details on client cache feature of NCache.
NCache client cache Equipped Web Roles for .NET
Here is a step by step guide on how to use NCache client cache feature in Windows azure ASP.NET Web role.
Configuring client cache in ASP.NET Web Role:
Step1: Develop an NCache equipped ASP.NET Web role and test It (without Client cache): Follow the steps explained in using NCache in Windows Azure Web role section to build and test an ASP.NET web role that has NCache assemblies referred and it successfully uses NCache for object and session caching.
You need to make sure that your Web role works fine for both object and session caching and it successfully adds data to Democache. (Democache name is used for clustered cache)
You will add client cache configurations to it later on.
Step2: Create a client cache: Client cache runs on client machines and if it is configured In-proc then it will run within Web role process and is automatically started when application starts. However, it requires configurations that are used for its instantiation.
You need to manually add a Cache configuration section so that your application has client cache configurations and it is automatically instantiated as soon your application starts using NCache.
Here is a sample client cache tag that you use as a reference to add in NCache Config.ncconf file (already referred within Web role project). This will create a local In-proc client cache named “DemoClientCache”.
<cache-config name="democlientcache" inproc="True" config-id="0" last-modified="" type="client-cache" auto-start="True">
    <log enabled="True" trace-errors="True" trace-notices="False" trace-warnings="False" trace-debug="False"/>
    <perf-counters enabled="True"/>
    <compression enabled="False" threshold="100kb"/>
    <notifications item-remove="False" item-add="False" item-update="False" cache-clear="False"/>
    <cleanup interval="15sec"/>
    <storage type="heap" cache-size="250mb"/>
    <eviction-policy enabled="True" default-priority="normal" policy="lru" eviction-ratio="5%"/>
We will add client side settings to link and use it along with a clustered cache.
Step3: Add client side settings for client cache: NCache Client.ncconf file is used when a cache is initialized, client applications reads information from this file and then connects to specified cache.
You now need to create client side settings so that your client cache is linked as well as used along with clustered cache as soon as your application initializes the clustered cache.
Here is how you modify Client.ncconf file:
Step1: Add Client Cache client side settings: You need to add client side settings for your client cache. Add following tag in Client.ncconf file:    
<cache id="democlientcache" client-cache-id="" client-cache-syncmode="optimistic" default-readthru-provider="" default-writethru-provider="" load-balance="True">
      <server name=""/>
If you already know the IP address of your Web role VM then you can also use that IP address instead of local host.
Step2: Modify clustered Cache client side settings: It is also required to link the client cache with clustered cache so that client cache is automatically used when application uses the clustered cache for more performance.
You will update Clustered cache (e.g. democache) client side settings in Client.ncconf file as follows by specifying the client cache name.
<cache id="Democache" client-cache-id="democlientcache" client-cache-syncmode="optimistic" default-readthru-provider="" default-writethru-provider="" load-balance="True">
<server name=""/>
<server name=""/>
This will link your client cache (“democlientcache”) with your clustered cache ("DemoCache") and based on these client configurations client cache is used automatically along with clustered cache.
Step4: Verify working of Client cache by running application: You test your application by initializing clustered cache ("DemoCache") and then by adding some data to the clustered cache as follows.
  _cache = NCache.InitializeCache("DemoCache");
            //Add items in the cache
            for (int i=0; i<100;i++)
                _cache.Insert("Key" + i.ToString(), "DummyObject" + i.ToString());
//Get items from the cache
            for (int i=0; i<100;i++)
                _cache.Get("Key" + i.ToString());
This should automatically instantiate your client cache “democlientcache” and data is also added. Moreover, subsequent calls are served directly from client cache.
You can open windows performance monitor on the Web role VM to visualize NCache performance counters for both clustered cache and client cache. You can verify that a client cache instance is automatically created when you ran your application and data also gets added to this client cache instance.
See Also

© Copyright  2014 Alachisoft. All rights reserved.