NCache 4.6 - Online Documentation


NCache provides a variety of ways to manipulate the cache data suitable for different use cases in different environments. NCache provides a clustered environment where multiple clients can access cache data. In such situation, parallel requests may occur for same data changes e.g. multiple parallel threads from within multiple applications can issue an update call against the same data. Internally, NCache allows every request to execute as much in parallel as possible to get the best performance without sacrificing data integrity. Every update in the cache store is treated as atomic and even replication between active and mirror nodes follow the same strategy to guarantee data consistency across the entire cache cluster. Beside this, NCache provides with an API to lock specific cache data, so that only a single thread can update or read it.
There are two types of locking available with NCache:
  • Optimistic Locking (Cache Item Versioning)
  • Pessimistic Locking (Exclusive Locking)
Optimistic Locking (Cache Item Versioning)
In optimistic locking, NCache uses cache item versioning. Cache Item 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 tracking whether any change occurred in item or not. When an item is fetched from cache, its current version is also fetched at the same time.
Cache item version can be specified in data update call. This update call succeeds only if the current version of the cache item is the same as passed by the client otherwise, it fails. This is used when a user wants to update a cache item which he/she has previously read from cache. If someone else has updated the cache item, update should fail. In this way each client will update only the latest copy of the data and hence maintain data integrity throughout the cache.
The latest version of item can be retrieved from cache through GetIfNewer API. Through this method, the user provides the existing version and asks for the more latest version if any. In this way, it can track whether an item is updated in cache or not.
Pessimistic Locking (Exclusive Locking)
NCache provides a locking mechanism that exclusively locks the cached data. In this mechanism, APIs with lock handle should be used. A lock handle is a handle associated with every locked item in the cache, which is returned by locking API. A locked item can be fetched/updated or unlocked only when its lock handle is provided at API level. However, it should be done with care to avoid data integrity issues. This locking mechanism is useful in situations when the user wants to exclusively lock his/her cache data to prevent multiple clients to update it simultaneously.
Lock Expiration: Lock timeout can also be specified while locking a cached item. The lock timeout is the time interval after which lock will be automatically released if no explicit call is made for releasing the lock during time out interval. This will prevent data from being locked for infinite time.
Forceful Release of Locks: Situations can arise in distributed environments when an application which had acquired the lock on a cache item terminates abruptly. In such a situation, all the locks acquired by such an application should be released. NCache provides unlock API, which releases the cache item lock forcefully.
Special Consideration while using Locking API
NCache provides set of APIs with and without lock handle to fetch/update the cache item. APIs without lock handle ignores item locking. So you should use all locking APIs for data manipulation. For example, if an item is locked and an update API call is made which does not take lock handle as input parameter, then item will be updated in the cache irrespective of its locking state. Therefore, when using locking feature , only those API calls should be used which take lock handle as parameters. APIs which do not take lock handle can be used but with a lot of care so that it does not affect data integrity.
In case of eviction/expiration, NCache ignores locks - which means that a locked item can be removed as a result of expiration or eviction.
See Also