Alachisoft NCache 4.1 - Online Documentation

Locking Cached Items

 
Locking prevents multiple clients from updating the same data simultaneously and also provides data consistency. A proper locking mechanism is necessary to ensure data consistency when there is a possibility of multiple clients accessing and possibly modifying the same data at the same time.
NCache provides an exclusive lock mechanism where once a client request acquires a lock for a particular data item, it blocks every other client until lock is released. Locks can also be forcefully released. A lockId and lockdate is associated with every locked item and are verified every time a locked item is accessed.
Include the following import statement in your project for using locking:
 
import com.alachisoft.ncache.web.caching.*;
 
Following are the methods that can be used to implement locking in an application.
 
Method Name
Parameters
Description
Get
string key, Timespan lockTimeout, ref lockHandle lockHandle, bool acquireLock
Gets cached item and can also acquire a lock on it if value of acquireLock parameter is true. key is a key of item to get.
lockTimeout is time span after which the lock will be released automatically.
lockHandle is an object of the lockHandle class which contains all the information about lock.
acquireLock determines whether to acquire lock or not.
 
Cache _cache = NCache.initializeCache("myCache");
TimeSpan timeSpan = new TimeSpan();
timeSpan.setSeconds( 10 );
// Acquires lock for specified time
Object lockedItem = _cache.get("Customer:David:1001", timeSpan, new LockHandle(), true);
Insert
string key, CacheItem item, lockHandle lockHandle, bool releaseLock
Inserts an item in cache and can also release a lock if item was locked. key is a key of item to insert.
item is a an item to insert into cache.
lockHandle is an object of the lockHandle class which contains all information about the lock.
releaseLock determines whether to release a lock or not on successful insert.
 
Cache _cache = NCache.initializeCache("myCache");
CacheItemVersion cItem = _cache.insert("Customer:David:1001", new CacheItem("Can Contain any type"), new LockHandle(), true);
Remove
string key, lockHandle lockHandle
Removes an item from cache. key is a key of item to release lock.
lockHandle is an object of the lockHandle class which contains all information about the lock.
 
Cache _cache = NCache.initializeCache("myCache");
// Populates Cache
_cache.remove("Customer:David:1001", new LockHandle());
Lock
string key, Timespan lockTimeout, out lockHandle lockHandle
Locks cached item. key is a key of item to acquire a lock.
lockTimeout is time span after which the lock will be released automatically.
lockHandle is an object of the lockHandle class which contains all information about the lock.
Cache _cache = NCache.initializeCache("myCache");
TimeSpan lockForTime = new TimeSpan();
lockForTime.setSeconds( 10 );
bool isLocked = _cache.lock("Customer:David:1001", lockForTime, new LockHandle());
if (!isLocked) // if Lock is not acquired
{
// If lock was unsuccessful, lock.getLockId() returns the ID of the already existing lock
String lockId = lock.getLockId();
}
UnLock
string key
Forcefully unlocks the cached item. key is a key of item to release lock.
 
Cache _cache = NCache.initializeCache("myCache");
_cache.unlock("Customer:David:1001");
UnLock
string key, lockHandle lockHandle
Unlocks cached item. key is a key of item to release lock.
lockHandle is an object of the lockHandle class which contains all information about the lock.
 
Cache _cache = NCache.initializeCache("myCache");
_cache.unlock("Customer:David:1001", "specific Lock ID");
 
 
NCache will ignore the locks if other overloads of Get, Insert and Remove methods are called.
 
 
 
See Also
 
 
Copyright © 2005-2012 Alachisoft. All rights reserved.