After successful initialization of cache and gaining valid cache handle, Add operation can be performed. Add is the basic operation provided by NCache; data can be added/updated to the cache using multiple API calls.
NCache has a basic “key” and “value” structure for objects. Every object must have a unique string key associated with it. Every key has an “Atomic” occurrence in cache be it local or clustered. Even though cached keys are case sensitive in nature; if the user tries to add another key with same value an Operation failed Exception is thrown by the cache.
The data to be used must be .NET serializable otherwise NCache will throw a serialization exception. Data can also be added directly as value property of CacheItem class; internally data is saved as a CacheItem.
Additionally, NCache Dynamic Compact Serialization framework for custom objects can also be used. This custom framework results in cost effective serialization of registered classes.
|
NCache returns CacheItemVersion with every add/Insert call; in these examples, we have ignored CacheItemVersion.
|
Adding Objects to Cache
In order to add data in cache it must be ensured that the object is either .NET serialized or registered with NCache Compact serialization framework. 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 exist in cache
// however verify the failure reason
}
Update Objects in Cache
Similarly in order to update data previously added in cache, it must be ensure that the object is either 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
}
Adding Objects Using Cacheitem
Similarly in order to update data previously added in cache, it must be ensure that the object is either serialized. In this example, a new object is added with an existing key.
CacheItem is a custom class provided by NCache which can be used to add data to the cache. This class encapsulates data as its value property. CacheItem also lets you set additional specifications associated with an object as properties of this class.
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
product.UnitsInStock = 15;
CacheItem cacheItem = new CacheItem(product);
string key = "Product:" +product.ProductID ;
try
{
cache.Add(key, cacheItem);
}
catch (OperationFailedException ex)
{
// handle exception
}
|
For the remaining API usage, kindly refer to the relevant NCache features.
|
Updating Objects Using Cacheitem
In this example, a key is updated that has already been exisiting in cache with object set as a property of CacheItem.
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
product.UnitsInStock = 5; // updated units
CacheItem cacheItem = new CacheItem(product);
string key = "Product:" +product.ProductID ;
try
{
cache.Insert(key, cacheItem);
}
catch (OperationFailedException ex)
{
// handle exception
}
Updating Data Using Cache Indexer
NCache being a key-value paired structure lets the user perform operations like any other indexed based data structure. In this example a cache handle is used to directly access and update cache values using square brackets ”[]”.
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
product.UnitsInStock = 5; // updated units
string key = "Product:" +product.ProductID ;
try
{
cache[key]=product;
}
catch (OperationFailedException ex)
{
//handle exception
|
For the remaining API usage, kindly refer to the relevant NCache features.
|
See Also