• Facebook
  • Twitter
  • Youtube
  • LinkedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Try NCache
  • Contact Us
Try NCache
Show / Hide Table of Contents

AppFabric Cache API vs. NCache API

This page compares AppFabric Cache APIs with the corresponding NCache APIs to help you migrate an existing application. It covers cache connections, CRUD operations, tag-based organization, locking, expiration, and event callbacks. Use these comparisons to identify the changes required when replacing AppFabric calls with NCache calls.

AppFabric Cache: Getting Cache Instance

In the AppFabric Cache, 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 Center or the PowerShell commands. 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

Pass the cache name to CacheManager.GetCache and configure the server endpoints in the ncache section of your application configuration, or supply them through ConnectionOptions. See Connecting to Cache.

CRUD Operations

Add Operation

  • AppFabric
  • NCache

In AppFabric Cache, an Add operation is as follows:

cacheInstance.Add(stringKey, stringValue);

NCache does not expose an AppFabric region parameter on ICache.Add. To represent a region as a Tag, create a CacheItem and set its Tags property:

var item = new CacheItem(stringValue)
{
    Tags = new[] { new Tag(regionName) }
};
cacheInstance.Add(stringKey, item);

Alternatively, if the application maps each region to a separate cache, connect to that cache and add the item:

regionNameasCacheInstance.Add(stringKey, stringValue);

Use the Tag approach for logical organization within one cache, or separate caches when the application requires separate cache instances.

Get Operation

The AppFabric and NCache Get operations are the same in syntax.

// Retrieve object
object value = 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:

  • AppFabric
  • NCache
cacheInstance.Put(key, value);
cacheInstance.Insert(key, value);

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, Remove<T> returns a Boolean result and provides the removed value through an out parameter. This functionality of NCache is similar to getting the item and deleting it from the cache.

  • AppFabric
  • NCache
cacheInstance.Remove(key, dataCacheItemVersion);

Use the generic Remove overload to obtain the removed value. Use the non-generic Remove overload when the value is not needed.

bool removed = cacheInstance.Remove<object>(key, out object removedValue, version: cacheItemVersion);

// Alternative: remove without returning the value.
cacheInstance.Remove(key, version: cacheItemVersion);

Further Get Operations

Bulk Get Operation

  • AppFabric
  • NCache

AppFabric has the following command to get multiple entries in the cache, returning a List KeyValuePair.

cacheInstance.BulkGet(listOfKeys, region);

In NCache, the Bulk command returns an IDictionary of <string, object> pair:

cacheInstance.GetBulk<T>(keys);

NCache accepts the cache keys as an IEnumerable<string>, so either a list or an array can be supplied.

GetIfNewer Operation

The GetIfNewer operations for AppFabric and NCache are the same.

GetObjectsByAllTags

  • AppFabric
  • NCache

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);

Use SearchService.GetByTags<T> with TagSearchOptions.ByAllTags. It accepts a collection of Tags and returns a dictionary of matching keys and typed values.

IDictionary<string, object> items = cacheInstance.SearchService.GetByTags<object>(arrayofTag, TagSearchOptions.ByAllTags);

GetObjectsByAnyTag Operation

For the corresponding NCache operation, use SearchService.GetByTags<T> with TagSearchOptions.ByAnyTag to return a dictionary of matching keys and values.

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
  • NCache

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);

For NCache, the same command is as follows:

object value = cacheInstance.Get<object>(key, true, timeout, ref lockHandle);

PutAndUnlock Operation

  • AppFabric
  • NCache

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);

NCache takes CacheItem as an argument which allows the user to add Tags directly to the item along with Group, Expiration, and Dependencies. The NCache overload is rich and provides more customizability.

cacheInstance.Insert(key, cacheItem, lockHandle, releaseLock: true);

Unlike AppFabric, which takes an object as the new value for the key, NCache provides a richer CacheItem to have an item in the cache that holds more metadata, which helps keep everything organized and structured.

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.

  • AppFabric
  • NCache
cacheInstance.Unlock(key, dataCacheLockHandle);
cacheInstance.Unlock(key, lockHandle);

ResetObjectTimeout Operation

  • AppFabric
  • NCache

AppFabric provides the functionality of resetting the expiration time of the item present in the cache by using the following function:

cacheInstance.ResetObjectTimeout(key, newTimeout);

In NCache, the client can update the item’s expiration through UpdateAttributes with a new Absolute Expiration:

var attributes = new CacheItemAttributes
{
    AbsoluteExpiration = absoluteExpiration
};
cacheInstance.UpdateAttributes(key, attributes);

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.

  • AppFabric
  • NCache
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. 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:

  • AppFabric
  • NCache
cacheInstance.AddCacheLevelCallback(dataCacheOperations, dataCacheNotificationCallback);
cacheInstance.MessagingService.RegisterCacheNotification(cacheDataNotificationCallback, eventType, EventDataFilter.DataWithMetadata);

As you can see, NCache provides a more comprehensive mechanism for registering 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 lets the user add a single callback with multiple types of events like the following modified command from above:

cacheInstance.MessagingService.RegisterCacheNotification(cacheDataNotificationCallback, EventType.ItemAdded | EventType.ItemUpdated, EventDataFilter.DataWithMetadata);

NCache also provides multiple callbacks to be registered for a single item.

NCache, however, does not provide alternatives to certain events supported by AppFabric, such as a failure notification callback in a failed operation triggers an event, and the user delegate's code is executed.

However, the user can find ways to mimic the same behavior by using the extensive functionality provided by NCache.

See Also

.NET: Alachisoft.NCache.Client namespace.
Java: com.alachisoft.ncache.client namespace.
Node.js: Cache class.
Python: ncache.client module.

Contact Us

PHONE

+1 214-619-2601   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Try NCache
Pricing
NCache Playground

Deployments
  • Cloud
  • Docker
  • Kubernetes
  • On-Premises
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • SignalR Backplane
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Blogs
  • Foundations
  • Videos
  • Comparisons
  • Shows
  • Whitepapers
  • Articles
  • Tech Talks
  • Magazine Articles
  • Third-Party Articles
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Docs
  • Schedule a Demo
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Terms of Use
  •  
  • Privacy Policy
  •  
  • Cookie Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
  • Facebook
  • Twitter
  • Youtube
  • LinkedIn
  • RSS