Alachisoft NCache 4.1 - Online Documentation

Cache Item Version

 
CacheItem Version is a property associated with every cache item. It is basically a numeric value that is used to represent the version of cached item which changes with every update to an item. This property allows you to track whether any changes occurred or not.
 
NCache first reads item version before updating it. It will update only if the item version is same i.e. only latest version is stored in the cache. As clusters are of distributed nature, so there could be a possibility that some cache clients might not have updated copy of item. So for concurrency reasons CacheItemVersion class is very important to be implemented. Following are the steps to use cache item version:
 
  1. Include the following namespace in your application.
     
    using Alachisoft.NCache.Web.Caching;
     
  2. Create a new Class named 'Customer' in your already created Test Application.
  3. Insert the following code in the 'Customer' class.
     
    [Serializable]
    class Customer
    {
    ...
    }
     
  4. Now insert the following code in the 'Main' of the project.
     
            Cache _cache = NCache.InitializeCache("mycache");
            _cache.Clear();
 
            Customer customer = new Customer();
            customer.name = "David";
            customer.customerID = 1001;
            customer.OrderPlaced = "C-11";
            customer.City = "New York";
 
            CacheItem cItem = new CacheItem(customer);
 
            //Add Item to Cache
            CacheItemVersion version1 = _cache.Add("Customer:David:1001", cItem);
 
            // Update the Cache Item, to change the CacheItemVersion
            customer.name = "David";
            customer.customerID = 1001;
            customer.OrderPlaced = "C-11";
            customer.City = "London";
 
 
GetIfNewer:
 
Here is how you can retrieve items from the cache
          
    //Update Cache Item Version
    CacheItemVersion version2 = _cache.Insert("Customer:David:1001", cItem);
    Object obj = _cache.GetIfNewer("Customer:David:1001", ref version1);
 
    if (obj == null)
    {
        Console.WriteLine("Value not updated");
    }
    else
    {
        Console.WriteLine("Current Version of Customer:David:1001 is:"+version1.Version.ToString());
    }
 
 
Remove:
 
Here is how you can remove items from the cache.
 
    //Update Cache Item Version
    CacheItemVersion version3 = _cache.Insert("Customer:David:1001", cItem);
 
    //Remove only if the local Client has the latest Version
    Object objGet = _cache.Remove("Customer:David:1001", version3);
    if (objGet == null)
    {
        Console.WriteLine("Item not removed from the cache. Newer Version of Item exists");
    }
    else
    {
        Console.WriteLine("Item Removed from the cache");
    }
 
 
Update:
 
Here is how you can get the latest items from the cache.
 
    //Update Cache Item Version
    CacheItemVersion version4 = cache.Insert("Customer:David:1001", cItem);
 
    //Get only if the local Client has the latest Version
    Object objGet = _cache.Get("Customer:David:1001", ref version1);
 
    if (objGet == null)
    {
        Console.WriteLine("Item version updated");
    }
    else
    {
        Console.WriteLine("Value not updated");
    }
 
 
5. Start the Local Cache 'myCache'.
6. Run the project.
 
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.