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 user to track any changes occurred (by some clients) to an item while it is been fetched and updated by other client.
 
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 import statements in your project for using item versioning:
     
    import com.alachisoft.ncache.web.caching.*;
    import java.util.*;
     
  2. Create a new Class named 'Customer' in your already created Test Application.
  3. Insert the following code in the 'Customer' class.
     
    import java.io.Serializable;
    public class Customer implements Serializable
    {
    ....
    }
     
  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.Id = 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.Id = 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", version1);
 
    if (obj == null)
    {
        System.out.println("Value not updated");
    }
    else
    {
        System.out.println("Current Version of Customer:David:1001 is: " + version1.getVersion());
    }
 
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)
    {
        System.out.println("Item not removed from the cache. Newer Version of Item exists");
    }
    else
    {
        System.out.println("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", version1);
 
    if (objGet == null)
    {
        System.out.println("Item version updated");
    }
    else
    {
        System.out.println("Value not updated");
    }
 
 
5. Start the Local Cache 'myCache'.
6. Run the project.
 
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.