Class NCache
Provides static methods and properties to aid with clustered cache initialization and access. This class cannot be inherited.
Inheritance
Namespace:
Assembly: Alachisoft.NCache.Web.dll
Syntax
public sealed class NCache
Remarks
In a Web application more than one instances of a clustered cache can be created per application domain, and they remain valid as long as the application domain remains active. A clustered cache makes sharing and caching data in a cluster as simple as on a single server. In addition to a clustered cache that resides on multiple nodes, NCache also makes it possible for different applications or application domains in a single application to share and cache data seamlessly.
From a development perspective NCache provides a simple interface for integration with application. A call to InitializeCache(String) requires just a registration id. The registration id is specified at the time of desgining the cluster or cache. See the documentation for NCache for more information on designing and implementing clusters. A scheme like this shields the development process from the complexities of cluster designs. Moreover it is possible to run the same application on different underlying cache types without actually changing a single line in the source code.
Caches
Returns the collection of clustered caches for this application.
Declaration
public static CacheCollection Caches { get; }
Property Value
Type | Description |
---|---|
CacheCollection | The instance of CacheCollection for this Web application. |
Remarks
One instance of this class is created per application domain, and it remains valid as long as the application domain remains active. Information about an instance of this class is available through the Caches property of the NCache object.
ExceptionsEnabled
Flag that indicates whether exceptions are enabled or not.
Declaration
public static bool ExceptionsEnabled { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean | true if exceptions are enabled, otherwise false. |
Remarks
If this property is set the Cache object throws exceptions from public operations. If not set no exception is thrown and the operation fails silently. Setting this flag is especially helpful during development phase of application since exceptions provide more information about the specific causes of failure.
Examples
This sample shows how to set the ExceptionsEnabled property.
NCache.Cache.ExceptionsEnabled = true;
InitializeCache(String)
Initializes the instance of Cache for this application.
Declaration
public static Cache InitializeCache(string cacheId)
Parameters
Type | Name | Description |
---|---|---|
System.String | cacheId | The identifier for the Cache item to initialize. |
Returns
Type | Description |
---|---|
Cache |
Remarks
The cacheId
parameter represents the registration/config id of the cache.
Depending upon the configuration the Cache object is
created inproc or outproc.
As this overload does not take Alachisoft.NCache.Web.Security.SecurityParams, internally it tries to load this information from "client.ncconf" file. For more details see NCache Help Collection.
Calling this method twice with the same cacheId
increments the reference count
of the cache. The number of InitializeCache(String) calls must be balanced by a corresponding
same number of Alachisoft.NCache.Web.Caching.Cache.Dispose(System.Boolean) calls.
Multiple cache instances can be inititalized within the same application domain. If multiple cache instances are initialized, Alachisoft.NCache.Web.Caching.NCache.Cache refers to the first instance of the cache.
Note: When starting a Cache as outproc, this method attempts to start NCache service on the local machine if it is not already running. However it does not start the cache automatically.
Examples
This sample shows how to use the InitializeCache(String) method inside a sample Web application.
public override void Init()
{
// A cache with id 'myCache' is already registered.
try
{
Alachisoft.NCache.Web.Caching.Cache theCache = NCache.InitializeCache("myCache");
}
catch(Exception e)
{
// Cache is not available.
}
}
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
InitializeCache(String, CacheInitParams)
Initializes the instance of Cache for this application.
Declaration
public static Cache InitializeCache(string cacheId, CacheInitParams initParams)
Parameters
Type | Name | Description |
---|---|---|
System.String | cacheId | The identifier for the Cache item to initialize. |
CacheInitParams | initParams | Holds the initialization parameters for Cache. |
Returns
Type | Description |
---|---|
Cache |
Remarks
The cacheId
parameter represents the registration/config id of the cache.
Calling this method twice with the same cacheId
increments the reference count
of the cache. The number of InitializeCache(String) calls must be balanced by a corresponding
same number of Alachisoft.NCache.Web.Caching.Cache.Dispose(System.Boolean) calls.
Multiple cache instances can be inititalized within the same application domain. If multiple cache instances are initialized, Alachisoft.NCache.Web.Caching.NCache.Cache refers to the first instance of the cache.
Note: When starting a Cache as outproc, this method attempts to start NCache service on the local machine if it is not already running. However it does not start the cache automatically.
Examples
This sample shows how to use the InitializeCache(String) method inside a sample Web application.
public override void Init()
{
// A cache with id 'myCache' is already registered.
try
{
CacheInitParams initParams = new CacheInitParams();
initParams.BalanceNodes = true;
initParams.ConnectionRetries = 5;
initParams.Mode = CacheMode.OutProc;
initParams.MultiPartitionConnection = false;
initParams.OperationTimeout = 30;
initParams.Port = 9900;
initParams.PrimaryUserCredentials = new Alachisoft.NCache.Web.Security.SecurityParams("domain\\user-id", "password");
initParams.RetryInterval = 5;
initParams.SecondaryUserCredentials = new Alachisoft.NCache.Web.Security.SecurityParams("domain\\user-id", "password");
initParams.Server = "server";
Alachisoft.NCache.Web.Caching.Cache theCache = NCache.InitializeCache("myCache", initParams);
}
catch(Exception e)
{
// Cache is not available.
}
}
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
InitializeCache(String, CacheInitParams, String, CacheInitParams)
Initializes the instance of Cache for this application.
Declaration
public static Cache InitializeCache(string remoteCacheId, CacheInitParams remoteInitParams, string clientCacheId, CacheInitParams clientInitParams)
Parameters
Type | Name | Description |
---|---|---|
System.String | remoteCacheId | The identifier for the remote Cache item to initialize. |
CacheInitParams | remoteInitParams | Holds the initialization parameters for the remote Cache. |
System.String | clientCacheId | The identifier for the client Cache item to initialize. |
CacheInitParams | clientInitParams | Holds the initialization parameters for the client Cache. |
Returns
Type | Description |
---|---|
Cache |
Remarks
The remoteCacheId
parameter represents the registration/config id of the cache.
Calling this method twice with the same remoteCacheId
increments the reference count
of the cache. The number of InitializeCache(String) calls must be balanced by a corresponding
same number of Alachisoft.NCache.Web.Caching.Cache.Dispose(System.Boolean) calls.
Multiple cache instances can be inititalized within the same application domain. If multiple cache instances are initialized, Alachisoft.NCache.Web.Caching.NCache.Cache refers to the first instance of the cache.
Note: When starting a Cache as outproc, this method attempts to start NCache service on the local machine if it is not already running. However it does not start the cache automatically.
Examples
This sample shows how to use the InitializeCache(String) method inside a sample Web application.
public override void Init()
{
// A cache with id 'remoteCacheId' and 'clientCacheId' is already registered.
try
{
CacheInitParams remoteInitParams = new CacheInitParams();
remoteInitParams.BalanceNodes = true;
remoteInitParams.ConnectionRetries = 5;
remoteInitParams.Mode = CacheMode.OutProc;
remoteInitParams.MultiPartitionConnection = false;
remoteInitParams.OperationTimeout = 30;
remoteInitParams.Port = 9900;
remoteInitParams.PrimaryUserCredentials = new Alachisoft.NCache.Web.Security.SecurityParams("domain\\user-id", "password");
remoteInitParams.RetryInterval = 5;
remoteInitParams.SecondaryUserCredentials = new Alachisoft.NCache.Web.Security.SecurityParams("domain\\user-id", "password");
remoteInitParams.Server = "remoteserver";
CacheInitParams clientInitParams = new CacheInitParams();
clientInitParams.Mode = CacheMode.InProc;
Alachisoft.NCache.Web.Caching.Cache theCache = NCache.InitializeCache("remoteCache", remoteInitParams, "clientCache", clientInitParams);
}
catch(Exception e)
{
// Cache is not available.
}
}
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
InitializeCache(String, String)
Initializes the instance of Cache for this application. Initializes client cache that keeps synchronized with remote cache.
Declaration
public static Cache InitializeCache(string remoteCacheId, string clientCacheId)
Parameters
Type | Name | Description |
---|---|---|
System.String | remoteCacheId | Level 2 cache id |
System.String | clientCacheId | Level 1 cache id |
Returns
Type | Description |
---|---|
Cache | Cache instance |
Remarks
The remoteCacheId
parameter represents the registration/config id of the cache.
The Cache object can only be created as outproc. An exception
will be thrown otherwise.
The clientCacheId
parameter represents the registration/config id of the cache.
Depending upon the configuration the Cache object is
created inproc or outproc.
Examples
This sample shows how to use the InitializeCache(String) method inside a sample Web application.
public override void Init()
{
// A remote cache with id 'myReplicatedCache' and client cache with id 'myCache' is already registered.
try
{
Alachisoft.NCache.Web.Caching.Cache theCache = NCache.InitializeCache("myReplicatedCache","myCache");
}
catch(Exception e)
{
// Cache is not available.
}
}
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
System.ArgumentNullException |
|