NCache 4.6 - Online Documentation

Sample Usage of Write-Through with Basic Operations

 
This feature is not available in NCache Express and Professional edition.
 
This section will explain the use of Write-Through provider after configuring and deploying it. NCache supports multiple Write-Through providers with applications.
 
NCache provides Alachisoft.NCache.Web.Caching.DSWriteOption enum to specify Write thru/Write behind options in APIs. Multiple  Write-Through providers can be configured through NCache. Default provider will be called if specific provider name is not mentioned through API. You can also use providers other than default by using provider specific overloads of APIs.
 
      To utilize Read-Through Caching, include the following namespaces in your application:
  • Alachisoft.NCache.Runtime.DatasourceProviders
  • Alachisoft.NCache.Web.Caching
  • Alachisoft.NCache.Runtime
 
 
  • Adding with Write-Through
 
Member
Description
CacheItemVersion Add(string key, CacheItem item, DSWriteOption dsWriteOption, DataSourceItemsAddedCallback onDataSourceItemAdded)
Adds item in cache and uses default provider
CacheItemVersion Add(string key, CacheItem item, DSWriteOption dsWriteOption, string providerName, DataSourceItemsAddedCallback onDataSourceItemAdded)
Adds item in cache and uses specified provider
 
 
            Product product = new Product();
            product.ProductID = 1001;
            product.ProductName = "Chai";
 
            CacheItem cacheItem = new CacheItem(product);
 
            string key = "Product:" + product.ProductID;
 
            try
            {
                CacheItemVersion itemVersion = cache.Add(key, cacheItem, DSWriteOption.WriteThru, null);
            }
            catch (OperationFailedException exp)
            {
                // handle exception
            }
 
 
  • Updating with Write-Through
 
Member
Description
CacheItemVersion Insert(string key, CacheItem item, DSWriteOption dsWriteOption, DataSourceItemsAddedCallback onDataSourceItemAdded)
Inserts item in cache and uses default provider
CacheItemVersion Insert(string key, CacheItem item, DSWriteOption dsWriteOption, string providerName, DataSourceItemsUpdatedCallback onDataSourceItemUpdatedCallback)
Inserts item in cache and uses specified provider
 
            Product product = new Product();
            product.ProductID = 1001;
            product.ProductName = "Chai";
 
            CacheItem cacheItem = new CacheItem(product);
 
            string key = "Product:" + product.ProductID;
 
            CacheItemVersion updatedItemVersion;
 
            try
            {
                updatedItemVersion = cache.Insert(key, cacheItem, DSWriteOption.WriteThru, "XmlWriteThruProvider", null);
 
                if (updatedItemVersion.Version > 1)
                {
                    //Item updated in cache. Perform further tasks.
                }
            }
            catch (OperationFailedException exp)
            {
                //handle exception
            }
 
  • Deleting an Existing Item with Write-Through
   
Member
Description
Delete(string key, DSWriteOption dsWriteOption, DataSourceItemsRemovedCallback onDataSourceItemRemovedCallback)
Deletes item from cache and uses default provider
Delete(string key, DSWriteOption dsWriteOption, string providerName, DataSourceItemsRemovedCallback onDataSourceItemRemovedCallback)
Deletes item from cache and uses specified provider
 
 
            string key = "Product:1001";
            try
            {
                cache.Delete(key, DSWriteOption.WriteThru, null);
            }
            catch (OperationFailedException exp)
            {
                // handle exception
            }
 
 
  • Removing an Existing Item with Write-Through
 
Member
Description
object Remove(string key, DSWriteOption dsWriteOption, DataSourceItemsRemovedCallback onDataSourceItemRemovedCallback)
Removes item from cache and uses default provider
object Remove(string key, DSWriteOption dsWriteOption, string providerName, DataSourceItemsRemovedCallback onDataSourceItemRemovedCallback)
Removes item from cache and uses specified provider.
 
            string key = "Product:1001";
 
            Product product = null;
 
            try
            {
                Object returnObject = cache.Remove(key, DSWriteOption.WriteThru, null);
 
                //Verify the removed item from both cache and data source
                if (returnObject != null)
                {
                    product = (Product)returnObject;
                }
            }
            catch (OperationFailedException exp)
            {
                // handle exception
        }
 
 
  • Adding New Item Asynchronously
 
Member
Description
AddAsync(string key, CacheItem item, DSWriteOption dsWriteOption, DataSourceItemsAddedCallback onDataSourceItemAdded)
Uses default provider and add item in cache asynchronously.
 
            Product product = new Product();
            product.ProductID = 1001;
            product.ProductName = "Chai";
 
            CacheItem cacheItem = new CacheItem(product);
 
            string key = "Product:" + product.ProductID;
 
            try
            {
                cache.AddAsync(key, cacheItem, DSWriteOption.WriteThru, null);
            }
            catch (OperationFailedException exp)
            {
                //handle exception
            }
 
 
  • Adding with Write-Behind
 
Member
Description
CacheItemVersion Add(string key, CacheItem item, DSWriteOption dsWriteOption, string providerName, DataSourceItemsAddedCallback onDataSourceItemAdded)
Adds item in cache by using Custom Provider and Write-Behind.
 
i. Add without Callback Method
 
            string key = "Customer:David:1001";
            CacheItem cacheItem = new CacheItem(new Product());
 
            try
            {
                cache.Add(key, cacheItem, DSWriteOption.WriteBehind, null);
            }
            catch (Exception exp)
            {
                //Operation fails if the item already exists
            }
 
 
ii. Add with Callback Method
 
        protected void OnDataSourceItemsAdded(IDictionary iDict)
        {
            //Perform appropriate actions upon response
        }
 
        private void AddTest()
        {
            string key = "Customer:David:1001";
 
            CacheItem cacheItem = new CacheItem(new Product());
 
            try
            {
                cache.Add(key, cacheItem, DSWriteOption.WriteBehind, OnDataSourceItemsAdded);
                //Verify item added in cache and in main source
            }
            catch (Exception exp)
            {
                //Operation fails if the item already exists
            }
        }
 
 
  • Updating Existing Data with Write-Behind
 
Member
Description
CacheItemVersion Insert(string key, CacheItem item, DSWriteOption dsWriteOption, string providerName, DataSourceItemsUpdatedCallback onDataSourceItemUpdatedCallback);
Adds item in cache by using default provider and Write-Behind.
CacheItemVersion Insert(string key, CacheItem item, DSWriteOption dsWriteOption, string providerName, DataSourceItemsUpdatedCallback onDataSourceItemUpdatedCallback);
Adds item in cache by using custom provider and Write-Behind.
 
protected void OnDataSourceItemsUpdated(IDictionary iDict)
        {
            //Perform appropriate actions upon response
        }
        private void UpdateTest()
        {
            string key = "Customer:David:1001";
 
            cacheItem.SubGroup = "WriteThru-Test";
 
            try
            {
                cache.Insert(key, cacheItem, DSWriteOption.WriteBehind, OnDataSourceItemsUpdated);
            }
            catch (Exception exp)
            {
                //handle exception
            }
        }
 
  • Deleting Existing Data with Write-Behind
   
Member
Description
Delete(string key, DSWriteOption dsWriteOption, DataSourceItemsRemovedCallback onDataSourceItemRemovedCallback);
Deletes item in cache with default provider and Write-Behind option.
Delete(string key, DSWriteOption dsWriteOption, string providerName,DataSourceItemsRemovedCallback onDataSourceItemRemovedCallback);
Deletes item in cache with custom provider and Write-Behind option.
 
        protected void OnDataSourceItemsRemoved(IDictionary iDict)
        {
            //Perform appropriate actions upon response
        }
 
        private void DeleteTest()
        {
            string key = "Customer:David:1001";
            try
            {
                cache.Delete(key, DSWriteOption.WriteBehind, OnDataSourceItemsRemoved);
                //Verify the removed item from both cache and data source
            }
            catch (OperationFailedException exp)
            {
                //Operation fails if key does not exist in the cache
            }
        }
 
 
  • Asynchronous Operations with Write-Behind
 
i. Add New Item Asynchronously
 
Member
Description
AddAsync(string key, CacheItem item, DSWriteOption dsWriteOption, DataSourceItemsAddedCallback onDataSourceItemAdded);
 
Add item asynchronously in cache by using default provider and Write-Behind option.
void AddAsync(string key, CacheItem item, DSWriteOption dsWriteOption, string providerName, DataSourceItemsAddedCallback onDataSourceItemAdded)
Add item asynchronously in cache by using custom provider and Write-Behind option.
 
        protected void OnDataSourceItemsAdded(IDictionary iDict)
        {
            //Perform appropriate actions upon response
        }
 
        private void AsyncAddTest()
        {
            try
            {
                string key = "Customer:David:1001";
 
                CacheItem cacheItem = new CacheItem(new Product());
 
                cache.AddAsync(key, cacheItem, DSWriteOption.WriteBehind, OnDataSourceItemsAdded);
                //verify that the key is added in Cache and master source
            }
            catch (Exception exp)
            {
                //handle exception
            }
        }
 
ii. Update Existing Item Asynchronously
 
Member
Description
void InsertAsync(string key, CacheItem item, DSWriteOption dsWriteOption, DataSourceItemsUpdatedCallback onDataSourceItemUpdatedCallback)
 
Updates existing item asynchronously in cache by using default provider
void InsertAsync(string key, CacheItem item, string providerName, DSWriteOption dsWriteOption, DataSourceItemsUpdatedCallback onDataSourceItemUpdatedCallback)
Updates existing item asynchronously in cache by using custom provider
 
        protected void OnDataSourceItemsUpdated(IDictionary iDict)
        {
            //Perform appropriate actions upon response
        }
 
        private void AsyncUpdateTest()
        {
            Cache mycache = NCache.InitializeCache("myreplicatedcache");
            try
            {
                string key = "Customer:David:1001";
 
                CacheItem cacheItem = new CacheItem(new Product());
                cacheItem.SubGroup = "Async-WriteThru-Test";
 
                cache.InsertAsync(key, cacheItem, DSWriteOption.WriteBehind, OnDataSourceItemsUpdated);
                //verify that the key is updated in Cache and master source
            }
            catch (Exception exp)
            {
                //handle exception
            }
        }
 
 
  • Bulk Operations with Write-Behind
 
i. Add New Items Using AddBulk Call
   
Member
Description
IDictionary AddBulk(string[] keys, CacheItem[] items, DSWriteOption dsWriteOption, DataSourceItemsAddedCallback onDataSourceItemsAdded)
Adds items in cache by using default provider
IDictionary AddBulk(string[] keys, CacheItem[] items, DSWriteOption dsWriteOption, string providerName, DataSourceItemsAddedCallback onDataSourceItemsAdded)
Adds item in cache by using specific provider
 
        protected void OnDataSourceItemsAdded(IDictionary iDict)
        {
            //Perform appropriate actions upon response
        }
 
        private void BulkAddTest()
        {
            try
            {
                String[] keys = { "Customer:David:1001", "Customer:Paul:1002", "Customer:Dave:1003", "Customer:Mathew:1004" };
 
                //create custom objects of Product type
                CacheItem[] data = GetCacheItems(keys.Length);
 
                IDictionary failedItems = cache.AddBulk(keys, data, DSWriteOption.WriteBehind, OnDataSourceItemsAdded);
                //verify updated items in the main source
            }
            catch (Exception exp)
            {
                //handle exception
            }
        }
 
 
ii. Update Existing Items Using InsertBulk API
 
Member
Description
IDictionary InsertBulk(string[] keys, CacheItem[] items, DSWriteOption dsWriteOption, DataSourceItemsUpdatedCallback onDataSourceItemUpdatedCallback)
Updates items in cache by using default provider and providing callback
IDictionary InsertBulk(string[] keys, CacheItem[] items, DSWriteOption dsWriteOption, string providerName, DataSourceItemsAddedCallback onDataSourceItemsAdded)
Updates items in cache by using specific provider and providing callback
 
        protected void OnDataSourceItemsUpdated(IDictionary iDict)
        {
            //Perform appropriate actions upon response
        }
 
        private void BulkUpdateTest()
        {
            try
            {
                String[] keys = { "Customer:David:1001", "Customer:Paul:1002", "Customer:Dave:1003", "Customer:Mathew:1004" };
 
                //create new  custom objects for above mentioned keys of Product type
                CacheItem[] data = cache.GetCacheItems(keys.Length);
 
                IDictionary failedItems = cache.InsertBulk(keys, data, DSWriteOption.WriteBehind, OnDataSourceItemsUpdated);
 
                //verify updated items in the main source
 
            }
            catch (Exception exp)
            {
                //handle exception
            }
        }
 
 
iii. Delete Existing Items Using DeleteBulk API
 
Member
Description
void DeleteBulk(string[] keys, DSWriteOption dsWriteOption, DataSourceItemsRemovedCallback onDataSourceItemsRemovedCallback)
Deletes items in cache by using default provider and providing callback
 
        protected void OnDataSourceItemsRemoved(IDictionary iDict)
        {
            //Perform appropriate actions upon response
        }
 
        private void BulkDeleteTest()
        {
            try
            {
                String[] keys = { "Customer:David:1001", "Customer:Paul:1002", "Customer:Dave:1003", "Customer:Mathew:1004" };
 
                IDictionary failedItems = cache.DeleteBulk(keys, DSWriteOption.WriteBehind, OnDataSourceItemsRemoved);
                //verify items deleted from the main source
            }
            catch (Exception exp)
            {
                //handle exception
            }
        }
 
 
See Also