AppFabric API vs. NCache API
Given that AppFabric is an obsolete technology, there are many differences between both of them. For instance, AppFabric uses regions to define divisions in the caches. Regions in AppFabric Cache are logical collections for organizing and managing cached data. On the other hand, NCache, uses Tags to symbolize them. Tags are string-based identifiers that let you associate keyword(s) with your cache items to create a logical collection.
For example, if you add the string "key1" in the default cache in AppFabric, you can also add "key1" in a region created by you in the same cache. For NCache, you can use a tag with the cache item "key1" to simulate a region. Similarly, NCache is adept at overcoming such differences.
AppFabric: Getting Cache Instance
In AppFabric, the user has to create a DataCacheFactoryConfiguration
that supplies a DataCacheFactory
, and from this, they can get a cache instance. In NCache, the configurations are taken care of by the NCache Management Centre or the Command Line Tool. The following command will return an instance of the cache if it is running and initialized.
ICache cacheInstance = CacheManager.GetCache(cacheName);
There are many cache management functions available in this instance. This section compares some of the overloads of the APIs to get a brief idea of how things are different with NCache.
Note
Your application will fetch the cache name from the client.ncconf file, therefore, please ensure that it is present as a part of your deployment. If you do not have NCache installed on your client machine, you can use the client.ncconf file added to your application project when you install the NCache AppFabric wrapper NuGet package to configure the cache client to access the NCache cluster. If you have NCache installed on your machine, you can find this file at %NCHOME%\config.
CRUD Operations
Add Operation
In AppFabric, an Add
operation is as follows:
cacheInstance.Add(stringKey, stringValue);
Get Operation
AppFabric and NCache Get operations are the same in syntax.
// Retrieve object
Object object = cache.Get<Object>(key);
The GetCacheItem
command for AppFabric and NCache is also the same, with NCache having multiple overloads to filter the CacheItem
returned.
Put/Insert Operation
The AppFabric Put
operation is equivalent to NCache's Insert
method. The simplest overload for both NCache and AppFabric is the same:
Remove Operation
AppFabric and NCache have similar Remove
operations except for the return type. In AppFabric, the return type of a Remove
function is Boolean
, signifying whether the item is successfully removed or not. In NCache, the return type is object
, determining the object value associated with the removed key. This functionality of NCache is similar to getting the item and deleting it from the cache.
Further Get Operations
Bulk Get Operation
AppFabric has the following command to get multiple entries in the cache, returning a List KeyValuePair
.
cacheInstance.BulkGet(listOfKeys, region);
The argument type differs for both APIs as AppFabric requires a List, whereas NCache requires an Array.
GetIfNewer Operation
The GetIfNewer
operations for AppFabric and NCache are the same.
GetObjectsByAllTags
AppFabric returns a list of KeyValuePair<string, object>
and takes a list of DataCacheTag
items to fetch the items added to the cache with the corresponding tags.
cacheInstance.GetObjectsByAllTags(listOfDataCacheTag, region);
GetObjectsByAnyTag Operation
Same as above, the GetObjectsByAnyTag
returns a list in the case of AppFabric and a Hashtable
in the case of NCache.
GetObjectsByTag
This operation takes a single DataCacheTag
in the case of AppFabric and a Tag
in the case of NCache.
GetObjectsInRegion
As mentioned earlier, NCache uses tags to represent regions. Therefore, to get all the objects in a region, NCache returns an enumerator, which you can iterate to get the key-value pairs present in the cache.
In the case of AppFabric, a DataCacheItemVersion
is returned, whereas in the case of NCache, CacheItemVersion
is returned, which are essentially the same (See documentation).
Locking and Unlocking Operations
GetAndLock Operation
AppFabric GetAndLock
operation is synonymous with the overload of Get operation of NCache, which takes a LockHandle
reference as an argument. For AppFabric, the command looks like:
cacheInstance.GetAndLock(key, timeout, out lockHandle);
PutAndUnlock Operation
AppFabric provides this functionality in one function where a locked item is unlocked and updated in the cache. The item can be updated with new DataCacheTags
as well. NCache achieves this by using an overload of the Insert function. A simple AppFabric command will look like the following in which the item is inserted with DataCacheTags
.
cacheInstance.PutAndUnlock(key, value, lockHandle, dataCacheTags);
Unlock Operation
AppFabric and NCache share the same functionality when it comes to unlocking an item present in the cache. Both use a key and a lock handle object to unlock the items.
ResetObjectTimeout Operation
AppFabric provides the functionality of resetting the expiration time of the item present in the cache by using the following function:
cacheInstance.ResetObjecTimeout(key, newTimeout)
This approach may seem more tedious when trying to maintain the value associated with the key at the client end. NCache provides a better alternative. The client can simply add the item with a Sliding Expiration. This is just like the expiration in AppFabric, but when an item is updated or fetched from the cache, its timeout is automatically reset. Therefore, the need for managing the timeouts of the objects is handled by NCache, making the user's job easier.
CallBack Registration
AppFabric allows the client to register event notifications on the cache and item levels. NCache provides a similar functionality. The difference occurs in the layout of the delegate used for either.
public delegate void DataCacheNotificationCallback(string cacheName, string regionName, string key, DataCacheItemVersion version, DataCacheOperations cacheOperation, DataCacheNotificationDescriptor nd);
The AppFabric delegate is verbose, whereas NCache is concise and to the point. The CacheEventArg
class contains all the required values, such as the value associated with the key, the metadata, and any other information for which the user has registered the event.
Registering a cache level callback is defined for each as follows:
cacheInstance.AddCacheLevelCallback(dataCacheOperations, dataCacheNotificationCallback);
See Also
.NET: Alachisoft.NCache.Client namespace.
Java: com.alachisoft.ncache.client namespace.
Node.js: Cache class.
Python: ncache.client class.