NCache 4.6 - Online Documentation

Add/Update Data to Cache

 
After successful initialization of cache and gaining a valid cache handle, you can perform an “Add operation”. Add is a basic operation provided by NCache, which allows you to add/update data to the cache using multiple API calls.
 
Key-Value cache store
NCache uses a “key” and “value” structure for objects. Every object must have a unique string key associated with it. Every key has an “Atomic” occurrence in the cache whether it is local or clustered. Cached keys are case sensitive in nature, and if you try to add another key with same value, an OperationFailedException is thrown by the cache.  
 
Custom Objects
The value of the objects stored in the cache can range from being simple string types to complex objects. However, the custom object data must be serializable otherwise NCache will throw a serialization exception. You can also add data using the setValue method of the CacheItem class, which results in the data being saved internally as a CacheItem.    
 
Additionally, you can use the NCache Dynamic Compact Serialization framework for custom objects. This framework provides cost effective serialization facilities for registered classes.
 
 
NCache returns CacheItemVersion with every add/Insert call; in these examples, we have ignored CacheItemVersion.
 
      To utilize the API, include the following namespace in your application: Alachisoft.NCache.Web.Caching.
 
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 exists 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
 
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
        }
 
 
Updating Objects Using Cacheitem
 
In this example, a key is updated that has already been existing 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 ”[]”.
 
        Cache cache = null;
        cache = NCache.InitializeCache("myreplicatedcache");
        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
        }
 
 
See Also