NCache 4.3 - Online Documentation

Locking Cached Items

 
Locking prevents multiple clients from updating the same data simultaneously and also ensures 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 the access for that item to every other client until the 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.
Following are the methods that can be used to implement locking in an application. For using locking include the following namespace in your application:
 
using Alachisoft.NCache.Web.Caching;
 
Method Name
Parameters
Description
Get
string key, Timespan lockTimeout, ref lockHandle lockHandle, bool acquireLock
Gets cached item and can also acquire lock on it if value of acquireLock parameter is set to 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 information about the lock.
acquireLock determines whether to acquire lock or not.
 
Cache _cache = NCache.InitializeCache("mycache");
// Acquires lock for specified time
LockHandle lockHandle = new LockHandle();
Object lockedItem = _cache.Get("key", new TimeSpan(0, 0, 5), ref 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 in 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");
LockHandle lockHandle = new LockHandle();
CacheItem cItem = new CacheItem ("my Item");
CacheItemVersion lockedItem = _cache.Insert("key", cItem, 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");
LockHandle lockHandle = new LockHandle ();
_cache.Remove("key", 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");
// Acquires lock for specified time
LockHandle lockHandle = new LockHandle ();
_cache.Lock("key", new TimeSpan (0, 0, 5), out lockHandle);
UnLock
string key
Forcefully unlocks the cached item. key is a key of item to release lock.
 
Cache _cache = NCache.InitializeCache("mycache");
_cache.Unlock("key");
UnLock
string key, lockHandle lockHandle
Unlocks the cached item. key is a key of item to release lock.
lockHandle is an object of the lockHandle class which contains all the information about lock.
 
Cache _cache = NCache.InitializeCache("mycache");
LockHandle lockHandle = new LockHandle();
_cache.Unlock("key", lockHandle);
 
NCache will ignore the locks if other overloads of Get, Insert and Remove methods are called.
 
 
See Also
 
© Copyright  2014 Alachisoft. All rights reserved.