• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Show / Hide Table of Contents
  • Programmer's Guide
  • Client Side API Programming
    • Setting Up Development Environment
    • Basic Cache Operations
      • Initialize Cache
      • Add Data to Cache
      • Update Data in Cache
      • Fetch Data From Cache
      • Remove Data From Cache
      • Dispose Cache
    • Bulk Operations
      • Adding Collection to Cache
      • Updating Collection in Cache
      • Retrieving Collection from Cache
      • Removing Collection from Cache
      • Deleting Collection from Cache
    • Asynchronous Operations
      • Using Asynchronous Operations
      • Using Asynchronous Operations with Callback Methods
    • Groups and Subgroups
      • Adding/Updating Data Group in Cache
      • Retrieving Data Group from Cache
      • Removing Data Group from Cache
    • Tagging Data in NCache
      • Creating Tags
      • Adding Items with Tags
      • Retrieving Previously Tagged Data
      • Removing Tagged Items from Cache
    • Named Tags
    • Data Expiration Strategies
      • Using Absolute Expiration
      • Using Sliding Expiration
    • Cache Dependencies
      • Key Dependency
      • File Dependency
      • Notification based Dependencies
        • Database Dependency using SQL Server
        • Database Dependency using Oracle
      • Polling Based Dependency
      • Custom Data Source Dependency
      • Multiple Cache Sync Dependency
      • Aggregate Dependency
      • Add Dependency to Existing Item
      • Using CLR Procedures to Call NCache
    • Locking Data in NCache
      • Locking Items in Cache (Pessimistic Locking)
      • Locking Items with Cache Item Versioning (Optimistic Locking)
    • SQL Reference for NCache
      • SQL Syntax
      • Querying Samples for Operators
      • Querying Data in NCache
      • NCache Language Integrated Query (LINQ)
        • Using LINQ in NCache
        • Configuring LINQPad for NCache
        • Querying NCache Data in LINQPad
    • Event Notifications
      • Cache Level Event Notifications
      • Item Level Event Notifications
      • Custom Event Notifications
    • Publish/Subscribe (Pub/Sub) in NCache
      • Pub/Sub Topics
      • Managing Topics
      • Pub/Sub Messages
        • Message Behavior and Properties
        • Creating a Message
      • Publish Messages to Topic
      • Subscribe for Topic Messages
      • Monitoring Pub/Sub Topics
    • Continuous Query
    • Using Streams in NCache
      • Opening with Stream Modes
      • Adding and Updating Data with Streams
      • Retrieving Data from Streams
      • Closing a Stream
    • Security and Encryption
      • NCache Security
      • NCache Data Encryption
    • Data Compression
    • NCache Management API
  • Server Side API Programming
    • Cache Startup Loader
      • Components of Cache Startup Loader
      • Sample Implementation of ICacheLoader on Single Node
      • Sample Implementation of ICacheLoader with Distribution Hints
    • Data Source Providers (Backing Source)
      • Read-Through Caching
        • Configure Read-Through Provider
        • Using Read-Through with Cache Operations
      • Write-Through Caching
        • Configuring Write-Through Provider
        • Using Write-Through with Basic Operations
        • Using Write-Behind with Basic Operations
        • Using Write-Behind with Bulk Operations
        • Using Write-Behind with Async Operations
        • Monitor Write-Through Counters
    • Custom Dependency
      • Sample Implementation of Custom Dependency
      • Sample Usage of Custom Dependency
    • WAN Replication through Bridge
      • Bridge Configurations
      • Implementing Bridge Conflict Resolver
    • Entry Processor
      • Sample Implementation of IEntryProcessor Interface
      • Sample Usage of EntryProcessor
    • MapReduce
      • Sample Implementation of MapReduce Interfaces
      • Sample Usage of MapReduce
    • Aggregator
      • Sample Implementation of IValueExtractor Interface
      • Sample Implementation of IAggregator Interface
      • Sample Usage of Aggregator
    • Dynamic Compact Serialization
  • Client Side ASP.NET Features
    • ASP.NET
      • ASP.NET Session State Provider for NCache
      • Multi-Region ASP.NET Session State Provider for NCache
    • ASP.NET Core
      • Session Storage in ASP.NET Core
        • Configure NCache ASP.NET Core Session Provider
        • Configure ASP.NET Core Sessions with NCache IDistributedCache Provider
      • Multi-Region ASP.NET Core Session Provider for NCache
      • Object Caching in ASP.NET Core
    • ASP.NET SignalR
      • Using NCache Extension for SignalR
    • View State Caching
      • Configuring and Using Content Optimization
      • Group View State with Sessions
      • Limit View State Caching
      • Perform Page Level Grouping for View State
    • ASP.NET Output Cache
      • Configure ASP.NET Output Caching
      • Using ASP.NET Output Cache with Custom Hooks
  • Client Side Third Party Integrations
    • Migrating AppFabric to NCache
      • AppFabric API vs. NCache API
    • NHibernate
      • NCache as NHibernate Second Level Cache
      • Using NHibernate Query Caching
      • Configuring Database Synchronization with NHibernate
    • Entity Framework Caching Integration
      • NCache as Entity Framework Second Level Cache
      • Entity Framework Caching Config File
    • Entity Framework Core Caching
      • Installing NCache Entity Framework Core Provider
      • Configuring NCache Entity Framework Core Provider
      • Using NCache Entity Framework Core Provider
        • Caching Options for EF Core Provider
        • LINQ APIs for EF Core Provider
        • Cache Only APIs for EF Core Provider
        • Query Deferred APIs for EF Core Provider
      • Logging in NCache Entity Framework Core Provider
    • Memcached
      • NCache Memcached Gateway Approach
      • Memcached Client Plugin for .NET
    • Debug NCache Providers in Visual Studio
    • NCache for Visual Studio Extension

Initialize Cache

After successfully configuring NCache, you can start developing applications utilizing NCache by embedding NCache API calls.

To utilize the APIs, include the following namespace in your application: Alachisoft.NCache.Web.Caching.

Using Basic Initialize Method

NCache provides InitializeCache method to get an instance of a NCache’s cache, with support of method overloading accommodate multiple scenario handling. This method uses all the properties specified in the client configuration file to initialize the cache, unless specified explicitly through API.

Cache cache = null;
string cacheName = "demoCache";
try{
    cache = NCache.InitializeCache(cacheName);
}
catch (Exception ex){
    //handle exception

    // Possible Exceptions:
    //1. No server available to process the request
    //2. client.ncconf does not contain current cache information
}

Initializing Multiple Caches in Single Application

For this exercise, both caches need to be configured previously and they must be in running state.

Cache cache1 = null;
Cache cache2 = null;
string cacheName1 = "demoCache";
string cacheName2 = "myreplicatedcache";
try{
        cache1 = NCache.InitializeCache(cacheName1);
        cache2 = NCache.InitializeCache(cacheName2);
}
catch (Exception ex){
            //handle exception
}

Initializing InProc Cache

Initializing an InProc cache is similar to initializing an OutProc cache except the fact that the InProc cache is not started.

Cache cache = null;
string localCacheName = "myLocalCache";

// Assumption: Cache ID has already been registered and is running
try{
    cache = NCache.InitializeCache(localCacheName);
}
 catch (Exception exp) {
      //handle exception
}

Initializing Cache Using CacheInitParams

CacheInitParams allows editing values of cache properties at initialization time. These values are the same which can be configured through client.ncconf file.

Warning

Any configuration specified through CacheInitParams will override the value in client.ncconf for that particular client.

In this example, the values of RetryInterval, ConnectionRetries, EnableKeepAlive and KeepAliveInterval properties can be changed; for this application these values will be used instead of the ones specified in client configuration file.

Cache cache = null;
try {
    // Create new InitParam instance
    CacheInitParams initParam = new CacheInitParams();
    initParam.RetryInterval = 3;
    initParam.ConnectionRetries = 2;
    initParam.EnableKeepAlive = true;
    initParam.KeepAliveInterval = 30;

    string cacheName = "demoCache";

    cache = NCache.InitializeCache(cacheName, initParam);
}
catch (Exception exp) {
    // handle exception
}

Initializing Cache with Security Credentials

If security has been enabled, you need to provide the security credentials while initializing caches so that the authorized user can perform the operation. For more details on Security and using security in NCache, see NCache Security.

To utilize the APIs, include the following namespace in your application: Alachisoft.NCache.Web.Security

CacheInitParams initParam = new CacheInitParams();
initParam.PrimaryUserCredentials = new SecurityParams(userName, password);

Cache cache = NCache.InitializeCache(cacheName, initParam);

Initializing Remote and Client Cache Simultaneously

Warning

In case you have configured the client cache using NCache Manager ,the basic public static Cache initializeCache(String cacheId) call would initialize both remote cache and client cache.

If separate calls are made for both caches, the local cache does not behave as a client cache - instead it functions as an independent local cache.

This API would return a handler of the remote cache, synchronizing all operations of the client cache.

Cache cache = null;
string remoteCache = "demoRemoteCache";
string clientCache = "demoClientCache";
try {
    cache = NCache.InitializeCache(remoteCache, clientCache);
}
catch (Exception ex){
    // handle exception
}

Similarly, use following API to initialize remote and client caches with CacheInitParams.

public static Cache InitializeCache(string remoteCacheId, CacheInitParams remoteInitParams, string clientCacheId, CacheInitParams clientInitParams);

Troubleshooting

Error: "No server available to process request"

Sometimes a cluster can not be created because the Server and Cluster ports are blocked by firewall. Following error message can occur in this case:

Error: "No server available to process request"

Workaround

Make sure that the Server port (at which NCache Service starts and accepts different client connections) is not blocked by firewall. Default value of this port is '9800'. You can change the default value from Alachisoft.NCache.Service.exe.config file.

  • NCache Manager unable to connect with NCache Service:
    If NCache Manager is unable to connect with NCache Service, even when it is started, then your system firewall might be blocking Service port. Unblock Service port from system firewall.

Error: "Unable to communicate with server node"

Cluster nodes can also be unable to communicate with each other because of the aforementioned reason as well.

Error: "Unable to communicate with server node"

Workaround

See if your firewall is allowing the Cluster port. Cluster port is a port at which nodes in a cluster communicate. You can change the default value of these ports from NCache Manager. If you have specified a port range in NCache Manager, unblock all ports in range.

Client Socket Deadlock

You may experience a deadlock situation on your client socket because of a long wait for a response from the cache server. This may also result in a lot of waiting threads that may cause performance issues.

Workaround

Step1: Changes on Server Side

Go to Alachisoft.NCache.Service.exe.config located at %NCHOME%/bin/service:

Add the following lines in the configuration file:

<add key = "NCacheServer.EnableBadClientDetection" value = "true" />

The value "true" indicates that BadClientDetection is enabled on your client socket. This means that now if a deadlock situation arises on your client socket, the socket will be reset. The default value of this property is "false".

<add key = "NCacheServer.ClientSocketSendTimeout" value = "10" />

When you have enabled BadClientDetection, you can specify the time interval after which the socket is reset in case of a deadlock. The default value of this property is "10" and this value can not be less than 1.

A few other properties have also been introduced in Alachisoft.NCache.Service.exe.config to help with this issue. Please configure these properties as follows:

<add key ="NCacheServer.EventPriorityRatio" value="30"/>

On the server-side a priority queue is used for events and cache operations. You can configure a ratio of events and cache operations for this queue through the property called EventPriorityRatio. The default value of this property is 30 which indicates a ratio 30:70 for events and cache operations. The value of this property can not be less than 1.

<add key ="NCacheServer.EventBulkCount" value="50"/>

The cache server now sends events to clients in bulk. Through this property you can set the number of items to be sent in bulk. By default this value is 50 and the value can not be less than 1. This is available in client version 4124 and above.

<add key ="NCacheServer.EventBulkCollectionInterval" value="2"/>

This property is used to set the time interval, in seconds, after which bulk of events is fired even in case the bulk count does not reach the EventBulkCount that the user has specified. Default value for this property is 2 seconds and the value can not be less than 1.

Restart the NCache service for these changes to take effect.

Step2: Changes on Client Side

Please make the following changes in your App.config/Web.config file:

<configuration>
    <appSettings>
          <add key ="NCacheClient.AsynchronousEventNotification" value="true"/>
          <add key ="NCacheClient.NumberofEventProccesingThreads" value="2"/>
     </appSettings>
</configuration>

AsynchronousEventNotification: Specifies whether events will be fired asynchronously or synchronously on client side. The default value is "true" which indicates that events will be fired asynchronously. The value "false" means that events will be fired synchronously on client side.

NumberofEventProcessingThreads: When the user has configured events to be fired synchronously, this flag is used to specify the number of threads which will process these events on client side. A very large value can cause performance issues.

Back to top Copyright © 2017 Alachisoft