• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

Management Operations on Cache

Note

This feature is only available in NCache Enterprise Edition.

NCache Management API is a set of programmable method calls that enable the user to perform basic management operations on OutProc caches without using either NCache Web Manager or NCache PowerShell cmdlets. This Management API is usually self-sufficient when executing a request.

Prerequisites

  • .NET/.NET Core
  • Java
  • To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
  • For API details, refer to: CacheConnection, CacheManager, CacheConnectionOptions, CacheClientConnectivityChanged, CacheClientConnectivityChangedCallback, ClientInfo, ConnectedClientList, GetCacheHealth, StartCache, StopCache, GetCache.
  • To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
  • For API details, refer to: CacheConnection, CacheManager, CacheConnectionOptions, onClientConnectivityChanged, ClientInfo, getConnectedClientList, CacheHealth, startCache, stopCache, getCache.

Start Cache Using API Calls

The StartCache method enables the user to start an OutProc cache on the specified server.

Start Cache

The cache starts using the StartCache method by using the server IP and cache name.

In this example, the user must specify the cache and server IP to start a node.

  • .NET/.NET Core
  • Java
try
{
    // Initialize cache name and server info

    string cacheName = "demoCache";
    string serverIp = "20.200.20.45";
    int port = 8250;

    // Provide server info to CacheConnection
    var connection = new CacheConnection(serverIp, port);

    // Use CacheConnection to start cache
    CacheManager.StartCache(cacheName, connection);
}
catch (SecurityException ex)
{
    // Permission will be denied if security is enabled
}
catch (OperationFailedException ex)
{
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
}
catch (CacheException ex)
{
    if (ex.ErrorCode == NCacheErrorCodes.CACHE_ALREADY_RUNNING)
    {
        // The cache is already started
    }
    if (ex.ErrorCode == NCacheErrorCodes.CACHE_ID_NOT_REGISTERED)
    {
        // If specified wrong cache name
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentNullException or ArgumentException
}
try {
    // Initialize cache name and sever info
    String cacheName = "demoCache";
    String serverIp = "20.200.20.45";
    int port = 8250;

    // Provide server info to CacheConnection
    CacheConnection connection = new CacheConnection(serverIp, port);

    // Use CacheConnection to start cache
    CacheManager.startCache(cacheName, connection);

} catch (SecurityException exception) {
    // Permission will be denied if security is enabled
} catch (CacheException exception) {
    if (exception.getErrorCode() == NCacheErrorCodes.CACHE_ALREADY_RUNNING) {
        // Cache is already started
    }
    if (exception.getErrorCode() == NCacheErrorCodes.CACHE_ID_NOT_REGISTERED) {
        // Wrong cache name specified
    }
} catch (Exception exception) {
    // Generic exception like IllegalArgumentException or NullPointerException
}

Start Cache with Security Enabled

If management level security is enabled, the user needs to provide the user credentials to perform any management operations on the cache. The StartCache method lets you start a cache with user credentials.

The following example starts a cache with security enabled on it.

  • .NET/.NET Core
  • Java
try
{
    // Initialize cache name and server info

    string cacheName = "demoCache";
    string serverIp = "20.200.20.45";
    int port = 8250;

    // Initialize security credentials
    string username = "john_smith";
    string password = "12345";

    // Provide server info to CacheConnection
    var connection = new CacheConnection(serverIp, port);

    // Provide user credentials to CacheConnection
    connection.UserCredentials = new Credentials(username, password);

    // Start cache with the security credentials
    CacheManager.StartCache(cacheName, connection);
}
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 Timeout
    // Operation performed during state transfer
}
catch (CacheException ex)
{
    if (ex.ErrorCode == NCacheErrorCodes.CACHE_ALREADY_RUNNING)
    {
        // The cache is already started
    }
    if (ex.ErrorCode == NCacheErrorCodes.CACHE_ID_NOT_REGISTERED)
    {
        // If specified wrong cache name
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentNullException or ArgumentException
}
try {
    // Initialize cache name and server info
    String cacheName = "demoCache";
    String serverIp = "20.200.20.45";
    int port = 8250;

    // Initialize security credentials
    String userName = "john_smith";
    String password = "12345";

    // Provide server info to CacheConnection
    CacheConnection connection = new CacheConnection(serverIp, port);

    // Provide user credentials to CacheConnection
    Credentials credentials = new Credentials(userName, password);
    connection.setUserCredentials(credentials);

    // Start cache with security enabled
    CacheManager.startCache(cacheName, connection);

} catch (SecurityException exception) {
    // Permission denied: Wrong user id or password provided
} catch (CacheException exception) {
    if (exception.getErrorCode() == NCacheErrorCodes.CACHE_ALREADY_RUNNING) {
        // Cache is already started
    }
    if (exception.getErrorCode() == NCacheErrorCodes.CACHE_ID_NOT_REGISTERED) {
        // Wrong cache name specified
    }
} catch (Exception exception) {
    // Generic exception like IllegalArgumentException or NullPointerException
}

Stop Cache Using API Calls

The StopCache method stops a cache on a particular server. If users don't specify a server, the method reads information from the client configuration file. If called from the client node, the first server listed in the client configuration file associated with the cache stops. You can stop a cache with enabled as well as disabled security. Moreover, the cache may also stop gracefully by setting the gracefullyShutDownNode flag as true.

Stop Cache

This example stops cache on an already started node. Cache name and server IP to stop on given node needs to be specified.

  • .NET/.NET Core
  • Java
try
{
    // Initialize cache name and server info

    string cacheName = "demoCache";
    string serverIp = "20.200.20.45";
    int port = 8250;

    // Provide server info to CacheConnection
    var connection = new CacheConnection(serverIp, port);

    // Stop the cache with the cache name and server
    CacheManager.StopCache(cacheName, connection);
}
catch (SecurityException ex)
{
    // Permission will be denied if wrong userID or password is provided
}
catch(OperationFailedException ex)
{
    else
    {
        // Exception can occur due to:
        // Connection Failures
        // Operation Timeout
        // Operation performed during state transfer
    }
}
catch (CacheException ex)
{
    if (ex.ErrorCode == NCacheErrorCodes.CACHE_ID_NOT_REGISTERED)
    {
        // If specified wrong cache name
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentNullException or ArgumentException
}
try {
    // Initialize cache name and server info
    String cacheName = "demoCache";
    String serverIP = "20.200.20.45";
    int port = 8250;

    // Provide server info to CacheConnection
    CacheConnection connection = new CacheConnection(serverIP, port);

    // Stop cache
    CacheManager.stopCache(cacheName, connection, true);

} catch (SecurityException exception) {
    // Permission denied: Security enabled on cache
} catch (CacheException exception) {
    if (exception.getErrorCode() == NCacheErrorCodes.CACHE_ID_NOT_REGISTERED) {
        // Wrong cache name specified
    }
} catch (Exception exception) {
    // Generic exception like IllegalArgumentException or NullPointerException
}

Stop Cache With Enabled Security

The following example shows how to stop a cache on a particular server with enabled security by providing it with the user credentials.

  • .NET/.NET Core
  • Java
try
{
    // Initialize cache name and server info

    string cacheName = "demoCache";
    string serverIp = "20.200.20.45";
    int port = 8250;

    // Initialize security credentials
    string username = "john_smith";
    string password = "12345";

    // Provide server info to CacheConnection
    var connection = new CacheConnection(serverIp, port);

    // Provide user credentials to CacheConnection
    connection.UserCredentials = new Credentials(username, password);

    // Stop cache with the security credentials
    CacheManager.StopCache(cacheName, connection);

}
catch (SecurityException ex)
{
    // Permission will be denied if wrong userID or password is provided
}
catch (OperationFailedException ex)
{
    else
    {
        // Exception can occur due to:
        // Connection Failures
        // Operation Timeout
        // Operation performed during state transfer
    }
}
catch (CacheException ex)
{
    if (ex.ErrorCode == NCacheErrorCodes.CACHE_ID_NOT_REGISTERED)
    {
        // If specified wrong cache name
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentNullException or ArgumentException
}
try {
    // Initialize cache name and server info
    String cacheName = "demoaCache";
    String serverIP = "20.200.20.45";
    int port = 8250;

    // Initialize security credentials
    String userName = "john_smith";
    String password = "12345";

    // Provide server info to CacheConnection
    CacheConnection connection = new CacheConnection(serverIP, port);

    // Provide user credentials to CacheConnection
    Credentials credentials = new Credentials(userName, password);
    connection.setUserCredentials(credentials);

    // Stop cache with security credentials
    CacheManager.stopCache(cacheName, connection, true);

} catch (SecurityException exception) {
    // Permission denied: Wrong username or password provided
} catch (CacheException exception) {
    if (exception.getErrorCode() == NCacheErrorCodes.CACHE_ID_NOT_REGISTERED) {
        // Wrong cache name specified
    }
} catch (Exception exception) {
    // Generic exception like IllegalArgumentException or NullPointerException
}

Viewing Cache Status Using API calls

The GetCacheHealth method enables users to view the cache server node status. 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 demoCache, registered on the server node specified, and listens on the port number 8250.

Note

Make sure that you have included the following namespace in your program: Alachisoft.NCache.Runtime.CacheManagement

  • .NET/.NET Core
  • Java
try
{
    // Specify cache name and server info
    string cacheName = "demoCache";
    string serverIp = "20.200.20.45";
    int port = 8250;

    // Provide server info to CacheConnection
    var connection = new CacheConnection(serverIp, port);

    // You can also specify the port where the cache service will listen
    // Get cache health of the specified
    CacheHealth cacheHealth = CacheManager.GetCacheHealth(cacheName, connection);

    // Shows the cache name, status and topology of the cache
}
catch (OperationFailedException ex)
{
    else
    {
        // Exception can occur due to:
        // Connection Failures
        // Operation Timeout
        // Operation performed during state transfer
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentNullException or ArgumentException
}
try {
    // initialize cache name and server info
    String cacheName = demoCache;

    // get cache health of the specified cache
    CacheHealth cacheHealth = new CacheHealth();
    cacheHealth.setCacheName(cacheName);
    CacheStatus status = cacheHealth.getStatus();

} catch (Exception exception) {
    // Any generic exception like IllegalArgumentException or NullPointerException
}

Monitoring Clients Connected to Cache

NCache provides the feature of enabling Client Activity Events. Using these events, each client connects to a clustered cache and can subscribe to notify about the connect/disconnect events of other clients. Each client can also specify a process-level custom identifier that returns to the subscriber based on the clients requirements, it can connect to or disconnect from a clustered cache. Client Activity Events fire when a client connects to or disconnects from a specified cache. These events are registered against the specified cache and are handled in a user-defined callback method.

Connect to Cache

The AppName is a process level unique identifier which can be assigned at the time of cache connection. For this purpose, before connecting to the cache, create an instance of CacheConnectionOptions 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.

  • .NET/.NET Core
  • Java
try
{
    // Specify the cacheName
    string cacheName = "demoCache";

    // Create an instance of CacheConnectionOptions
    var options = new CacheConnectionOptions();

    // Assign the custom identifier
    options.AppName = "MyApp";

    // Connect to cache with custom AppName property
    ICache cache = CacheManager.GetCache(cacheName, options);

}
catch (OperationFailedException ex)
{
    // NCache specific exception
    if (ex.ErrorCode == NCacheErrorCodes.NO_SERVER_AVAILABLE)
    {
        // Make sure NCache Service is running
        // Make sure that the cache is running
    }
    else
    {
        // Exception can occur due to:
        // Connection Failures: ErrorCode 17506
        // Operation Timeout: ErrorCode 35003
        // Operation performed during state transfer
    }
}
catch (ConfigurationException ex)
{
    if(ex.ErrorCode == NCacheErrorCodes.SERVER_INFO_NOT_FOUND)
    {
        // client.ncconf must have server information
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentNullException or ArgumentException
    // Argument exception occurs in case of empty string name
}
try {
    // Specify the cache name
    String cacheName = demoCache;

    // Create an instance of CacheConnectionOptions
    CacheConnectionOptions options = new CacheConnectionOptions();

    // Assign the custom identifier
    options.setAppName("MyApp");

    // Connect to cache with custom appName property
    Cache cache = CacheManager.getCache(cacheName, options);

} catch (OperationFailedException exception) {
    if (exception.getErrorCode() == NCacheErrorCodes.NO_SERVER_AVAILABLE) {
        // Make sure that NCache service and cache are running
    } else {
        // Exception can occur due to:
        // Connection Failures: ErrorCode 17506
        // Operation Timeout: ErrorCode 35003
        // Operation performed during state transfer
    }
} catch (ConfigurationException exception) {
    if (exception.getErrorCode() == NCacheErrorCodes.SERVER_INFO_NOT_FOUND) {
        // client.ncconf must have server info
    }
} catch (Exception exception) {
    // Generic exception like IllegalArgumentException or NullPointerException
}

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:

  • .NET/.NET Core
  • Java
public delegate void CacheClientConnectivityChangedCallback(string cacheId, ClientInfo client);

The method created contains the following parameters:

// User defined event callback method
static void MyClientConnectivityCallback(string cacheName, ClientInfo clientInfo)
{
    // Handle event
}
public interface cacheClientConnectivityChangedCallback {
    void invoke(String cacheName, ClientInfo clientInfo);

    static void myClientConnectivityStatus(String cacheName, ClientInfo clientInfo) {
        // handle event
    }

    static void registerMethodToEvent(Cache cache) {
        ClientInfo clientInfo = cache.getClientInfo();
    }
}


Parameters Description
cacheName Name of the cache with which the clients are connected or disconnected. This acts as the sender of the event.
clientInfo An object of the ClientInfo class containing information regarding the client which caused the event to fire.

Client Info Class

Member Description
AppName User assigned, process-level, unique identifier
ClientID Unique id of client
IPAddress IP through which the client is connected
MacAddress Mac address of the client
PhysicalCores Available physical cores of the client
LogicalCores Available logical cores of the client
ProcessID Process ID of the client
MachineName Machine Name of the client
Status The connectivity status of the cache client.
ClientVersion The version of the cache client
IsDotNetCore Flag to check whether the client is .NET Core or not
Memory Available memory 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.MessagingService.CacheClientConnectivityChanged += MyClientConnectivityCallback;

Monitor Client Info of Connected Clients

The ClientInfo of each connected client is available locally in the form of cache.ClientInfo property.

View List of Connected Clients

To get information of all the clients connected to the cache on demand, use the ConnectedClientList property on the cache as:

IList<ClientInfo> connectedClients = cache.ConnectedClientList;

See Also

Start Cache
Stop Cache

Back to top Copyright © 2017 Alachisoft