NCache 4.4 - Online Documentation

Initialize Cache

 
After you have configured the cache, embed NCache API calls in your application to use it. The first thing you need to do is to initialize the cache. Include the following namespace in your project:
 
using Alachisoft.NCache.Web.Caching;
 
Using Basic Initialize Method
 
This method is the simplest provided by NCache to Initialize a cache. It uses all properties specified in the client configuration file to Initialize.
 
      Cache cache=null;
try
{
    cache = NCache.InitializeCache("mycache");
}
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 a 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;
try
{
    cache1 = NCache.InitializeCache("mycache");
    cache2 = NCache.InitializeCache("myreplicatedcache");
}
catch (Exception ex)
{
    //handle exception
}
 
Initializing InProc Cache
 
Initializing an in-proc cache is similar to initializing an OutProc cache except the fact that the InProc cache is not started. 
 
Cache cache = null;
// Assumption: Cache ID has already been registered and running
try
{
    cache = NCache.InitializeCache("myLocalCache");
}    
catch (Exception exp)
{
    //handle exception
}
 
Initializing Cache using CacheInitParams
 
InitParams allows editing values of the properties at initialization time. In this example, the values of RetryInterval and ConnectionRetries properties can be changed; for this application these values will be used instead of the ones specified in client configuration files.
 
Cache cache = null;
try
{    
    // Create new InitParam instance
    CacheInitParams initParam = new CacheInitParams();
    initParam.RetryInterval = 3;
    initParam.ConnectionRetries = 2;
    cache = NCache.InitializeCache("mycache", initParam);
}
catch (Exception exp)
{
    // handle exception
}
 
Initializing Remote and Client Cache Simultaneously
 
 
In case client cache is configured using NCache manager 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 remains a separate local cache. In order to use an existing local cache as a client cache instead of making separate Initialization calls, we recommend that you use.
 
public static Cache InitializeCache(string remoteCacheId, string clientCacheId);
This API would return  handler of the remote cache synchronizing all operations of the client cache.
Cache cache = null;
try
{
    cache = NCache.InitializeCache("myRemoteCache", "myClientCache");
}
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);
 
 
See Also