NCache API Caching
After successfully configuring NCache, you can start developing the application by embedding NCache API calls. NCache client applications can communicate with the cache servers through InitializeCache method. This method establishes connection between client and server and returns a cache handle. This cache handle can then be further used for Add
, Insert
, Get
, Remove
and other cache operations.
Let's quickly create a console .NET application to demonstrate the use of NCache.
Pre-requisites
Launch Visual Studio 2010+ for .NET Framework.
Create a new Console Application for .NET Framework.
For NCache OpenSource Edition, install the NuGet package Alachisoft.NCache.OpenSource.SDK on Package Manager Console as following:
Install-Package Alachisoft.NCache.OpenSource.SDK -Version 4.6.3
Ensure Data is Serialized
Whether you are using NCache for ASP.NET Sessions or for object caching, you must ensure that all .NET objects are serializable.
Select "Add->Class" menu from Solutions to create a new class Product as following:
[Serializable]
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public string Category { get; set; }
public int UnitsInStock { get; set; }
}
Initialize Cache
After you have configured the cache, embed NCache API calls in your application. The first thing you need to do is to initialize the cache with name of the cache you created in Create Cache.
//Initialize the cache
Cache cache = NCache.InitializeCache("demoClusteredCache");
Add Data
In order to add data to cache, it must be ensured that the object is either .NET
serialized. In this example, a new object of Product
class is created and added to cache.
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
product.UnitsInStock = 15;
product.Category = "Beverages";
string key = "Product:" + product.ProductID;
try
{
cache.Add(key, product);
}
catch (OperationFailedException ex)
{
// Handle exception
// usually thrown if key already exists in cache
// however verify the failure reason
}
Insert Data
Similarly, in order to update data previously added in cache, it must be ensured that the object is serialized. In this example, a new object is added with an existing key.
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
product.UnitsInStock = 5; // updated units
product.Category = "Beverages";
string key = "Product:" + product.ProductID;
try
{
// Precondition: Cache is already initialized and item exists
cache.Insert(key, product);
}
catch (OperationFailedException ex)
{
// Handle exception
}
Remove Data
NCache provides two methods (remove, delete) to delete an object from the cache.
Delete Method | Remove Method |
---|---|
Returns void after deletion. | Deletes data from the cache and returns status to the application. |
Using Remove Method
Remove
removes the key from the cache and returns the removed object to the cache.
string key = "Product:1001";
Product product = null;
try
{
// Null is returned if key does not exist in cache
object result = cache.Remove(key);
if (result != null)
{
if (result is Product)
{
product = (Product)result;
}
}
}
catch (OperationFailedException ex)
{
// Handle exception
}
Using Delete Method
Delete
deletes the key from the cache without returning the object.
string key = "Product:1001";
try
{
cache.Delete(key);
}
catch (OperationFailedException ex)
{
// Handle exception
}
Get Data
Get
method is used to retrieve item Product:1001
.
This item has previously been added to the cache. Get
method returns general
object which needs to be cast accordingly. If a key does not exist in cache, null
value is returned.
string key = "Product:1001";
Product product = null;
try
{
// Null is returned if key does not exist in the cache
object result = cache.Get(key);
if (result != null)
{
if (result is Product)
{
product = (Product)result;
}
}
}
catch (OperationFailedException ex)
{
// Handle exception
}
Dispose Cache
After you are done using the cache, you should dispose it in order to free the resources being used up by the cache.
Cache cache = NCache.InitializeCache("demoClusteredCache");
// Dispose the cache
cache.Dispose();