A lockHandle is associated with item to be locked. This ensures that the particular item remains inaccessible throughout the cache. Two primary properties lockId and lockdate are used for locking while performing various cache operations.
NCache provides method calls exclusively for locking as well as numerous overloads that manipulate the locking mechanism.
Locking an Item Explicitly
Lock on an item can be acquired explicitly before performing any operation. Lock method requires a
TimeSpan to lock an item for a specified time. However, if you do not want the acquired lock to expire simply specify a new
TimeSpan(). The
lock method used in this example associates a lockHandle. Kindly ensure that the single LockHandle is associated with a single key. Release the lock before re-using the handle; otherwise it might lead to inconsistency of behavior.
//create a new lock Handle
LockHandle lockHandle = new LockHandle();
string key="Product:1001";
try
{
// Specifiying the time span of 10 sec for which the item remains locked
bool locked = cache.Lock(key, new TimeSpan(0, 0, 10), out lockHandle);
}
catch (OperationFailedException ex)
{
// handle exception
}
Locking an Item During Fetch Operation
An item while being retrieved. The item will be inaccessible for others unless it is released. In case of a mismatch of key, null is returned.
In this example, a key and a lockHandle for the key should be provided to fetch the cached object and lock it. “True” should be given if the user wants to acquire lock.
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
}
Locking with Expiration
If time is specified as a value of TimeSpan(), NCache would lock the item for that specified duration.
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
{
bool lockAcquired = cache.Lock(key, lockSpan, out lockHandle);
//Verify that the lock is released automatically after this time period.
}
catch (OperationFailedException ex)
{
// handle exception
}
Releasing Lock with Update Operation
While updating an item, the lock can be released allowing other cache clients to use the cached data. In order to successfully release the locked item, you will lockHandle that was initially used to lock the item must be specified. In case of an invalid or different handle, NCache would throw an OperationFailedException.
Product updatedProduct = new Product();
updatedProduct.ProductID = 1001;
updatedProduct.ProductName = "Chai";
updatedProduct.Category = 4;
string key = "Product"+ updatedProduct.ProductID;
LockHandle lockHandle = new LockHandle();
try
{
// lock exisiting item for the time span of 30 seconds
bool locked = cache.Lock(key, TimeSpan.FromSeconds(30), out lockHandle);
if (locked)
{
cache.Insert(key, new CacheItem(updatedProduct), lockHandle, true);
}
}
catch (OperationFailedException ex)
{
// handle exception
}
Releasing Lock Explicitly
In order to release lock forcefully on a previously locked cached item, lockHandle that was used to lock the key should be specified.
LockHandle lockHandle = new LockHandle();
string key = "Product:1001";
try
{
// lock an existing item and save the lockHandle for 10 seconds
bool locked = cache.Lock(key, new TimeSpan(0, 0, 10), out lockHandle);
if (locked)
{
// unlock locked item using saved LockHandle
cache.Unlock(key, lockHandle);
}
}
catch (OperationFailedException ex)
{
// handle exception
}
See Also