NCache 4.6 - Online Documentation

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 scenarios’ handling. This method uses all the properties specified in the client configuration file to initialize the cache.
 
      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 InProc 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
 
CacheInitParams 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,
public static Cache InitializeCache (string cacheId) 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