NCache 4.6 - Online Documentation

Locking Item while Performing Fetch Operation

 
An item, while being retrieved, will be inaccessible for others unless it is released. In case of a mismatch of key, null is returned.
 
Fetching an item with locking can result in any of the possible cases:
 
Case 1: The result of the Get operation contains the item.
This implies that the lock was acquired and the item exists in the cache.
 
Case 2: The result returned is null.
This case can be caused in two situations:
1. The item does not exist.
In such a case, the lockHandle reference is initialized and returned as null.
 
2. The lockHandle provided is inconsistent with the one locking the item.
In this case, the lockHandle is updated with the latest lockID which can be used for future requests.
 
In this example, a key and a lockHandle for the key should be provided to fetch the cached object and lock it.
 
If the item being fetched is already locked by another application, the result is null. However, the lockHandle returned is updated with the existing lock object on the server side. The user can check whether the lock was acquired or not based on the result. If the result is null, it is up to the user to use the handle for future requests.
 
       To utilize the API, include the following namespace in your application: Alachisoft.NCache.Web.Caching.
 
            LockHandle lockHandle = new LockHandle();
 
            //Specify time span of 10 sec for which the item remains locked
            TimeSpan lockSpan = new TimeSpan(0, 0, 10);
 
            string key = "Product:1001";
 
            try
            {
                object result = cache.Get(key, lockSpan, ref lockHandle, true);
 
                if (result != null)
                {
                    if (result is Product)
                    {
                        Product product = (Product)result;
                    }
                }
            }
            catch (OperationFailedException ex)
            {
                // handle exception
            }
 
 
See Also