NCache API Caching
This section gives you a quick go through on how NCache API can be used to perform basic cache operations like add, insert, remove and get items from the cache.
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.
[Serializable]
public class Product
{
public Product()
{ }
string productName;
int productID;
int unitsInStock;
int category;
//getters setters
}
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;
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
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();