AppFabric API vs. NCache API
This feature is only available in NCache Enterprise Edition.
AppFabric uses regions to define divisions in the caches. NCache uses caches to symbolize them. As regions reside in a named cache and can have same yet unique keys, NCache uses caches to mimic this behavior.
For example, you add "key1" with a string value in the default cache in AppFabric. Now you can also add "key1" in a region created by you in the SAME cache. For NCache key is unique in a cache so in order to use multiple regions, multiple caches must be created.
Getting Cache Instance
In AppFabric the user has to create a
DataCacheFactoryConfiguration which needs
to be supplied to a
DataCacheFactory and from this factory object the user can
get a cache instance. In NCache, the configurations are taken care of by the
Manager or the command line tool and 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 through this instance. We are going to compare some of the overloads of the APIs to get a brief idea of how things are different with NCache.
In AppFabric a basic
Add operation looks like the following:
In NCache the basic command is no different but when you are specifying a region in the command like:
cacheInstance.Add(stringKey, stringValue, regionName);
In this case to achieve the same functionality as of a region the following command must be used:
You have to register the region as a cache in NCache otherwise you will not be able to use the above command.
Bulk Get Operation
AppFabric has the following command in order to get multiple entries residing in the cache.
This returns a
List KeyValuePair in AppFabric whereas in NCache the Bulk command
returns an IDictionary of <string, object> pair:
The argument type is also different for both APIs as AppFabric requires a List whereas NCache required an Array.
AppFabric Get operation and NCache Get operation are the same in syntax.
GetAndLock operation is synonymous to the overload of Get operation of
NCache which takes a reference of LockHandle as an argument. For AppFabric a
command looks like:
cacheInstance.GetAndLock(key, timeout, out lockHandle)
For NCache the same command is as follows:
cacheInstance.Get(key, timeout, ref this.lockHandle)
Both commands return an object as the value associated with the key.
GetCacheItem command for both AppFabric and NCache is the same with NCache
having multiple overloads to filter the
GetIfNewer operation for AppFabric and NCache are exactly the same.
AppFabric returns a list of
KeyValuePair<string, object> and takes a list of
DataCacheTag items to fetch the items that were added to the cache with the
corresponding tags. NCache has the
GetByAllTags command to substitute this
functionality. For NCache a
Hashtable is returned and the function takes an
Tags as an argument.
Same as above, the
GetObjectsByAnyTag returns a list in case of AppFabric and a
Hashtable in the case of NCache.
This operation takes a single
DataCacheTag in case of AppFabric and a
case of NCache.
As mentioned earlier NCache uses caches to represent region therefore in order to get all the objects present in a region/cache NCache can return an enumerator which you can iterate over to get the key value pairs present in the cache.
Put operation can be replaced with NCache's
Insert method. The
simplest overload for both NCache and AppFabric is the same:
In case of AppFabric a
DataCacheItemVersion is returned whereas in case of
CacheItemVersion is returned which are essentially the same (See
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. In NCache the updating after unlocking the item is
achieved by using an overload of the Insert function. A simple AppFabric command
will look like the following in which the item is inserted with
cacheInstance.PutAndUnlock(key, value, lockHandle, dataCacheTags);
CacheItem as argument which allows the user to add Tags directly to
the item along with Group. Sub-Group, Expiration and Dependencies. The NCache
overload is rich and provides more customizability.
cacheInstance.Insert(key, cacheItem, lockHandle, acquireLockBool)
Unlike AppFabric which takes an object as the new value for the key, NCache
provides the more rich
CacheItem to have an item in the cache that holds more
metadata that helps on forming organized and structured data groups.
AppFabric and NCache have similar
Remove operations except for the return type.
In case of AppFabric, the return type of a
Remove function is
signifies whether the item is successfully removed or not. In NCache, the return
object, which is to say the value of the object associated with the said
removed key. This functionality of NCache is similar to getting the item and
deleting it from cache.
In NCache you can use either Remove or Delete. If the client simply wants to delete the item without
fetching the item, NCache provides the
cacheInstance.Remove(key, cacheItemVersion) cacheInstance.Delete(key, cacheItemVersion)
AppFabric provides the functionality of resetting the expiration time of the item present in cache by using the following function:
In NCache the client can accomplish the same behavior by inserting the same item with the same value with a new absolute expiration:
cacheInstance.Insert(key, value, absoluteExpiration, null, CacheItemPriority.Default)
This may seem more tedious as to maintain the value associated with the key at the client end would require more resources. NCache provides a better alternative. The client can simply add the item with Sliding expiration. This is just like the expiration used in AppFabric but when an item is updated or fetched from the cache the timeout is automatically reset for the key. Therefore, the need for managing the timeouts of the objects is handled by NCache making the clients job less hectic.
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 respective item.
AppFabric allows the client to register event notification on cache and item level. NCache provides a similar behavior. The difference comes 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);
public delegate void CacheDataNotificationCallback(string key, CacheEventArg cacheEventArgs);
The AppFabric delegate is verbose whereas NCache is concise and to the point.
CacheEventArg class contains all the required values such as the value
associated with the key, the meta data and any other information for which the
client has registered the event for.
Registering a cache level callback is defined for each as follows:
cacheInstance.AddCacheLevelCallback( dataCacheOperations, dataCacheNotificationCallback)
cacheInstance.RegisterCacheDataNotification( cacheDataNotificationCallback, eventType, EventDataFilter.DataWithMetaData)
As you can see NCache provides a more comprehensive mechanism to register a notification callback. Also, note here that AppFabric allows one callback to be registered for only ONE kind of event meaning you can have one callback for Item added and another for Item updated. NCache allows the client to add a single callback with multiple types of events like the following modified command from above:
cacheInstance.RegisterCacheDataNotification( cacheDataNotificationCallback, EventType.ItemAdded | EventType.ItemUpdated, EventDataFilter.DataWithMetaData)
NCache also provides multiple callbacks to be registered for single item.
NCache, however, does not provide alternatives to certain events supported by AppFabric such as failure notification callback in a failed operation triggers an event and user delegate's code is executed.
However, the client can find ways to mimic the same behavior by using the extensive functionality provided by NCache.