NCache Management API
NCache Management API is a set of programmable method calls that enable the user to perform basic management operations on out-proc caches without using either NCache Manager or NCache tools. The Management API in some cases relies on the client configuration files to perform an operation, whereas in some cases it is self-sufficient to execute a request.
Pre-Requisites for using NCache Management API
To utilize NCache Management APIs, include the following namespace in your application:
Alachisoft.NCache.Web.Management
Alachisoft.NCache.Web.Caching
Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Start Cache Using API calls
StartCache
method enables the user to start an OutProc cache on the specified
server.
Start Cache
Cache can be started using the StartCache method which starts a cache by getting provided with the server and cache name.
In this example, the user needs to specify cache and server name to start a node.
try
{
// Specify the cache name and the server name
string cacheName = "myreplicatedcache";
string serverName = "20.200.20.30";
// Start cache with the start cache command
CacheManager.StartCache(cacheName, serverName);
// Cache will be started successfully
}
catch (SecurityException ex)
{
// Permission will be denied if security is enabled
}
catch (OperationFailedException ex)
{
if (ex.Message.Contains("Specified cacheId is not registered."))
{
// If specified wrong cache name
}
else
{
// Exception can occur due to:
// Connection failures
// Operation performed during state transfer
// Operation timeout
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Start Cache with Security Enabled
In case security is enabled; the user needs to provide the user credentials to perform any management operations on the cache. StartCache method lets you start a cache with user credentials.
The following example starts a cache with security enabled on it.
try
{
// Specify the cache name and the server name
string cacheName = "myreplicatedcache";
string serverName = "20.200.20.30";
// Specify the user credentials used for security
string username = "username";
string password = "password";
// Enter the security parameters
SecurityParams securityParams = new SecurityParams(username, password);
// Start cache with the start cache command
CacheManager.StartCache(cacheName, serverName, securityParams, null);
}
catch (SecurityException ex)
{
// Permission will be denied if wrong userID or password is provided
}
catch (OperationFailedException ex)
{
if (ex.Message.Contains("Specified cacheId is not registered."))
{
// If specified wrong cache name
}
else
{
// Exception can occur due to:
// Connection failures
// Operation performed during state transfer
// Operation timeout
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Stop Cache Using API calls
StopCache method is used to stop a cache on a particular server. You can stop a cache with enable as well as disabled security. Moreover cache can also be stopped gracefully by setting the ISGracefulShutdown
flag as true.
Stop Cache
StopCache
method is used to stop a cache on a particular server. If no server is
specified, the method would read information from client configuration file. If called from the client node, the first
server listed in the client configuration file associated with the cache will be
stopped.
This example stops cache on an already started node. Cache and server name to stop cache on given node need to be specified.
try
{
// Specify the cache name and the server name
string cacheName = "myreplicatedcache";
string serverName = "20.200.20.30";
// Stop the cache with the cache name and server
CacheManager.StopCache(cacheName, serverName);
}
catch (SecurityException ex)
{
// Permission will be denied if wrong userID or password is provided
}
catch(OperationFailedException ex)
{
// Exception can occur due to:
// Connection failures
// Operation performed during state transfer
// Operation timeout
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Stop Cache With Enabled Security
StopCache
method is used to stop a cache on a particular server with enabled security by providing it with the user credentials.
try
{
// Specify the cache name and the server name
string cacheName = "myreplicatedcache";
string serverName = "20.200.20.30";
// Specify the user credentials used for security
string username = "username";
string password = "password";
// Enter the security parameters
SecurityParams securityParams = new SecurityParams(username, password);
// Start cache with the start cache command
CacheManager.StopCache(cacheName, serverName, securityParams, null);
}
catch (SecurityException ex)
{
// Permission will be denied if wrong userID or password is provided
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection failures
// Operation performed during state transfer
// Operation timeout
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Viewing Cache Status Using API calls
GetCacheHealth
method enables the user to view the status of cache server nodes. This method
call returns a CacheHealth
object against every call.
You can specify the cache name, the server node and the management port to get the cache health information.
The following example gets the cache health for the cache myreplicatedcache, registered on the server node specified and listens on the port number 8250.
try
{
// Specify the cachename whose information is needed
string cacheName = "myreplicatedcache";
// Specify any of the server node of the specified cache
string initialNode = "20.200.20.30";
// You can also specify the port where the cache service will listen
// Get cache health of the specified
CacheHealth cacheHealth = CacheManager.GetCacheHealth(cacheName, initialNode);
// Shows the cache name, status and topology of the cache
}
catch (OperationFailedException ex)
{
if (ex.Message.Contains("cache not registered on specified node"))
{
// If the cachename is incorrect or does not exist
// If the servername is incorrect or does not exist
}
else if (ex.Message.Contains("No connection could be made because the target machine actively refused it"))
{
// If the port is not correctly provided
}
else
{
// Exception may occur due to:
// Connection failures
// Operation performed during state transfer
// Operation timeout
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Monitoring Clients Connected to Cache
NCache provides the feature of enabling Client Activity Events. Using these events, each client that is connected to a clustered cache can subscribe to be notified about the connect/disconnect events of other clients. Each client can also specify a process level custom identifier which will be returned to the subscriber in case the client which specified it connects to or disconnects from a clustered cache.
Client Activity Events are fired when a client connects to or disconnects from a specified cache. These events are registered against the specified cache and will be handled in a user defined callback method.
Pre-Requisites for Monitoring Connected Clients
To utilize NCache Management APIs, include the following namespace in your application:
Alachisoft.NCache.Web.Management
Alachisoft.NCache.Web.Caching
Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Initialize Cache
The AppName
is a process level unique identifier which can be assigned at the
time of initialization. For this purpose, before initializing the cache client,
create an instance of CacheInitParams
and assign a custom identifier string to
its AppName
property.
Thus, if this client connects or disconnects, the callbacks of all the clients
which subscribed to the Client will get this AppName
in their ClientInfo
instance.
try
{
// Specify the cacheName
string cacheName = "myPartitionedCache";
// Create an instance of CacheInitParams
CacheInitParams initParams = new CacheInitParams();
// Assign the custom identifier
initParams.AppName = "AssignedAppName";
//initialize cache with custom AppName property
Cache cache = NCache.InitializeCache(cacheName, initParams);
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection failures
// Operation performed during state transfer
// Operation timeout
}
catch (Exception ex)
{
// Any other generic exception like ArgumentNullException or ArgumentException
}
Create Event Callback Method
The callback method is user defined and is called once the clients are
connected/disconnected from the cache. The method is registered against
the CacheClientConnectivityChanged
event property
of CacheClientConnectivityChangedCallback
type in a specific cache. Hence, the
method created should match the signature of the delegate:
public delegate void CacheClientConnectivityChangedCallback(string cacheId, ClientInfo client, ConnectivityStatus status);
The method created contains the following parameters:
//user defined event callback method
static void MyClientConnectivityCallback(string cacheName, ClientInfo clientInfo, ConnectivityStatus status)
{
//handle event
}
Parameters | Description |
---|---|
cacheName |
Name of the cache with which the clients are connected or disconnected. This acts as the sender of the event. |
status |
Enum which determines the connectivity status of the client: Disconnected = 0 Connected =1 |
clientInfo |
An object of the ClientInfo class containing information regarding the client which caused the event to fire. |
Client Info class
Member | Description | Default Value |
---|---|---|
AppName |
User assigned, process-level, unique identifier | Client process ID |
ClientID |
Unique client identifier | - |
IPAddress |
IP through which the client is connected | - |
MachineName |
Machine Name of the client | - |
ProcessID |
Process ID of the client | - |
Register Event
The method is registered by appending it to
the CacheClientConnectivityChanged
property as an event callback. This enables
the client to listen for connect/disconnect events of other clients. If another
client connects to or disconnects from the specified cache, the callback method
is called.
//register method to event
cache.CacheClientConnectivityChanged += MyClientConnectivityCallback;
Monitor Client Info of Connected Clients
The ClientInfo
of each connected client is available locally in the form
of cache.ClientInfo
property.
ClientInfo info = cache.ClientInfo;
string AppName = info.AppName;
string clientID = info.ClientID;
System.Net.IPAddress ip = info.IPAddress;
string machineName = info.MachineName;
int processID = info.ProcessID;
View List of Connected Clients
To get information of all the clients connected to the cache on demand, use
the GetConnectedClientsList()
method on the cache as:
IList<ClientInfo> connectedClients = cache.GetConnectedClientsList();