Distributed Data Structures in NCache

NCache is an extremely fast and scalable Distributed Cache for .NET / .NET Core. NCache is used by high transaction .NET server applications for application data caching, ASP.NET / ASP.NET Core session storage, and Pub/Sub Messaging.

NCache has also become a really powerful In-Memory Distributed Datastore that is shared among multiple .NET applications. As a result, applications want to store more and more types of data in NCache to be shared among multiple applications. To address this need, NCache provides Distributed Data Structures in .NET to be shared across multiple users. They are:

Each of these data structures is described in more detail below.

Distributed Queue

A distributed queue allows you to create a queue to be shared among multiple .NET applications running in a distributed environment. And, the Queue ensures that the sequence in which you’ve entered data in it is maintained. And, all applications retrieving data from the Queue can rely on this sequence to be always correct.

string cacheName = "demoCache";
string QueueName = "DistributedQueue:Customers";
  
// Initialize an instance of the cache to begin performing operations:
ICache cache = NCache.Client.CacheManager.GetCache(cacheName);
  
// Creating a named distributed queue
IDistributedQueue<Customer> distQueue = 
	cache.DataTypeManager.CreateQueue<Customer>(QueueName);
  
distQueue.Enqueue(new Customer {
	ContactName = "David Johnes", 
	CompanyName = "Lonesome Pine Restaurant"
});
distQueue.Enqueue(new Customer {
	ContactName = "Carlos Gonzalez", 
	CompanyName = "LILA-Supermercado"
});

As you can see above, a Distributed Queue allows you to add (Enqueue) items in a sequence so you can later remove (Dequeue) them in the same sequence (FIFO). Here is the Distributed Queue interface:

public interface IDistributedQueue<T> : IEnumerable<T>, IEnumerable, ICollection,
IDistributedDataTypes, ILockable, INotifiable
    {
        void Clear();
        bool Contains(T item);
        void CopyTo(T[] array, int arrayIndex);
        
        T Dequeue();
        void Enqueue(T item);
        T Peek();
        T[] ToArray();
    }

As you can see in the above mentioned IDistributedQueue interface, it provides you all of the traditional Queue functionality.

Distributed HashSet

A Distributed HashSet behaves just like the .NET HashSet class but in a shared manner for multiple applications and users. And, a HashSet provides all Set operations like:

  • - No duplicates: in the Set
  • - Set operations: like Union, Intersection, Difference

And, being a shared HashSet for multiple applications or users, this becomes a powerful data structure to use. Here is an example of how to use it.

string cacheName = "demoCache";
string hashSetName = "DistributedHashSet:UniqueValueHashSet";

// Initialize an instance of the cache to begin performing operations:
ICache cache = NCache.Client.CacheManager.GetCache(cacheName);

// Creating distributed hashSet with absolute expiration
IDistributedHashSet<string> hashSet;
hashSet = cache.DataTypeManager.CreateHashSet<string>(hashSetName);

// Create data for hashset
var daysOfWeek = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday",
                                "Friday", "Saturday", "Sunday" };
// Add multiple entries to the hashset
hashSet.AddRange(daysOfWeek);

// Since this entry already exists, no change to the hashset is made by this call
hashSet.Add("Monday");

As you can see, when you add “Monday” the second time, it is not added to the HashSet.

Distributed Dictionary

A Distributed Dictionary (IDistributedDictionary) behaves just like the regular .NET IDictionary interface but in a shared manner for multiple applications and users. And, a Distributed Dictionary provides all Dictionary operations like Add, Update, Remove, Contains, and more.

On top of regular Dictionary features, Distributed Dictionary provides the ability to expire items based on NCache expiration options like Absolution Expiration and Sliding Expiration.

ICache cache = NCache.Client.CacheManager.GetCache("demoCache");
string dictName = "DistributedDictionary:Customers";

IDistributedDictionary<string, Customer> dict = cache.DataTypeManager.GetDictionary<string, Customer>(dictName);

	if (dict == null)
		{
			DataTypeAttributes attributes = new DataTypeAttributes {
				Expiration = new Expiration(ExpirationType.Absolute, new TimeSpan(0, 1, 0))
					};
      		// Creating distributed Dictionary with absolute expiration of 1 minute
      		dict = cache.DataTypeManager.CreateDictionary<string, Customer>(dictName, attributes);
		}

	Customer cust = new Customer
		{	
			CustomerID = "customer1",
			ContactName = "David Johnes",
			CompanyName = "Lonesome Pine Restaurant",
			ContactNo = "(1) 408-354-9768",
			Address = "Silicon Valley, Santa Clara, California",
		};
	   
dict.Add("customer1", cust);

Customer cachedCust;
bool fetched = dict.TryGetValue("customer1", out cachedCust);

As you can see, Distributed Dictionary behaves just like a regular .NET Distributed (IDictionary interface). But, behind the scenes it is distributed which makes is very scalable. And, it is also shared across multiple applications.

Distributed List

Distributed List behaves just like the regular .NET IList but with a difference that is it distributed and can be shared across multiple applications and processes. Distributed List is an unsorted list and you can add items either at the end of the list thru the “Add()” method or at any location through the “Insert()” method by providing an index.

Below is an example of how to use Distributed List.

ICache cache = NCache.Client.CacheManager.GetCache("demoCache");
string listName = "DistributedList:Customers";

IDistributedList<Customer> distList = 
	cache.DataTypeManager.GetList<Customer>(listName);

	if (distList == null)
		{
			DataTypeAttributes attributes = new DataTypeAttributes {
				Expiration = new Expiration(ExpirationType.Absolute, new TimeSpan(0, 1, 0))
					};

			// Creating distributed list with absolute expiration of 1 minute
			distList = cache.DataTypeManager.CreateList<Customer>(listName, attributes);
		}

	Customer cust = new Customer
		{
			CustomerID = "customer1",
			ContactName = "David Johnes",
			CompanyName = "Lonesome Pine Restaurant",
			ContactNo = "(1) 408-354-9768",
			Address = "Silicon Valley, Santa Clara, California",
		};

distList.Add(cust);

Customer cachedCustomer = distList[0];

You can also remove items from the list, iterate over it, and many more things.

Distributed Counter

Distributed Counter is a powerful data structure in NCache that allows you to maintain a unique counter in a distributed environment shared by multiple applications. This has a lot of uses and allows you to quickly develop applications around it. Below is an example:

ICache cache = NCache.Client.CacheManager.GetCache("demoCache");
string counterName = "DistributedCounter:Customers";

ICounter counter = cache.DataTypeManager.GetCounter(counterName);
	if (counter == null)
		{
		    DataTypeAttributes attributes = new DataTypeAttributes
			{
			    Expiration = new Expiration(ExpirationType.Absolute, new TimeSpan(0, 1, 0))
			};
			
		// Creating distributed counter with absolute expiration
		// to modify cache properties of the counter, provide an instance of DataTypeAttributes in the second parameter
		counter = cache.DataTypeManager.CreateCounter(counterName, attributes);
	}

counter.SetValue(1000);
long newValue = counter.IncrementBy(5);
newValue = counter.Value;
newValue = counter.DecrementBy(2);

What to Do Next?

NCache Docs
Edition Comparison
Request a Personalized LIVE Demo
Download NCache

Signup for monthly email newsletter to get latest updates.

© Copyright Alachisoft 2002 - . All rights reserved.