分布式数据结构 NCache

NCache 是一个极其快速且可扩展的 .NET/ 分布式缓存.NET Core、Java、Node.js 和 Python 应用程序。 NCache 由高事务 .NET 服务器应用程序用于应用程序数据缓存、ASP.NET / ASP.NET Core 会话存储和 Pub/Sub 消息传递。

NCache 也成为了一个真正强大的内存分布式数据存储,可以在多个 .NET 和 Java 应用程序之间共享。 因此,应用程序希望在其中存储越来越多类型的数据 NCache 在多个应用程序之间共享。 为了解决这个需求, NCache 提供 Java 和 .NET 中的分布式数据结构,以便在多个用户之间共享。 他们是:

下面更详细地描述这些数据结构中的每一个。

 

分布式队列

分布式队列允许您创建一个在分布式环境中运行的多个 .NET 和 Java 应用程序之间共享的队列。 并且,队列确保您在其中输入数据的顺序得到维护。 并且,从队列检索数据的所有应用程序都可以依赖此序列始终正确。

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"
});
String cacheName = "demoCache";
String queueName = "DistributedQueue:Customers";

// Initialize an instance of the cache to begin performing operations:
Cache cache = CacheManager.getCache(cacheName);

// Creating a named distributed queue
DistributedQueue<Customer> distQueue = cache.getDataStructuresManager().createQueue(queueName, Customer.class);

distQueue.add(new Customer("David Johnes", "Lonesome Pine Restaurant"));
distQueue.add(new Customer("Carlos Gonzalez", "LILA-Supermercado"));

正如您在上面看到的,分布式队列允许您按顺序添加(入队)项目,以便以后可以按相同的顺序(FIFO)删除(出队)它们。 这是分布式队列接口:

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();
}
public interface DistributedQueue<T> extends Queue<T>, DistributedDataStructure, Notifiable 
{
	void clear();
	void copyTo(T[] array, int arrayIndex);

	T peek();

	public interface Queue<E> extends Collection<E> {
		boolean add(E e);
		E remove();
	}

	public interface Collection<E> extends Iterable<E> {
		boolean contains(Object o);
	}
}

正如您在上面提到的 IDistributedQueue 接口中所看到的,它为您提供了所有传统的队列功能。

 

分布式哈希集

分布式 HashSet 的行为类似于 .NET HashSet 类,但以共享方式供多个应用程序和用户使用。 而且,HashSet 提供了所有的 Set 操作,例如:

  • - 没有重复: 在集合中
  • - 设置操作: 比如并集、交集和差集

而且,作为多个应用程序或用户的共享 HashSet,这成为一种强大的数据结构。 这是一个如何使用它的示例。

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");
String cacheName = "demoCache";
String hashSetName = "DistributedHashSet:UniqueValueHashSet";

// Initialize an instance of the cache to begin performing operations:
Cache cache = CacheManager.getCache(cacheName);

// Creating distributed HashSet with absolute expiration
DistributedHashSet<String> hashSet;
hashSet = cache.getDataStructuresManager().createHashSet(hashSetName, String.class);

// Add entries to the hashset
hashSet.add("Monday");
hashSet.add("Tuesday");
hashSet.add("Wednesday");
hashSet.add("Thursday");
hashSet.add("Friday");
hashSet.add("Saturday");
hashSet.add("Sunday");

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

如您所见,当您第二次添加“Monday”时,它并没有添加到 HashSet 中。

 

分布式字典

分布式字典 (IDistributedDictionary) 的行为类似于常规 .NET IDictionary 接口,但以共享方式供多个应用程序和用户使用。 而且,分布式字典提供所有字典操作,如添加、更新、删除、包含等。

在常规字典功能之上,分布式字典提供了根据 NCache 到期选项如 绝对到期滑动到期.

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);
String cacheName = "demoCache";
String dictName = "DistributedDictionary:Customers";
 
// Initialize an instance of the cache to begin performing operations:
Cache cache = CacheManager.getCache(cacheName);

DistributedMap<string, Customer> dict = cache.getDataStructuresManager().createMap(dictName, Customer.class);

Customer customer = new Customer("David Johnes", "Lonesome Pine Restaurant", "Customer1", "(1) 408-354-9768");

dict.put("customer1", customer);
Customer cachedCustomer = dict.get("customer1");

正如您所看到的,分布式字典的行为就像常规的 .NET 分布式(IDictionary 接口)一样。 但是,在幕后,它是分布式的,这使得它非常可扩展。 而且,它还可以在多个应用程序之间共享。

 

分布式列表

分布式列表的行为与常规 .NET IList 类似,但不同之处在于它是分布式的并且可以在多个应用程序和进程之间共享。 分布式列表是一个未排序的列表,您可以通过以下方式在列表末尾添加项目 加() 方法或在任何位置通过 插入() 通过提供索引的方法。

下面是如何使用分布式列表的示例。

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];
String cacheName = "demoCache";
String listName = "DistributedList:Customers";

// Initialize an instance of the cache to begin performing operations:
Cache cache = CacheManager.getCache(cacheName);

DistributedList distributedList = cache.getDataStructuresManager().createList(listName,   Customer.class);

Customer customer = new Customer("David Johnes", "Lonesome Pine Restaurant", "Customer1", "(1) 408-354-9768");

distributedList.add(customer);
Customer cachedCustomer = (Customer) distributedList.get(0);

您还可以从列表中删除项目、对其进行迭代以及执行许多其他操作。

 

分布式计数器

分布式计数器是一种强大的数据结构 NCache 它允许您在多个应用程序共享的分布式环境中维护唯一的计数器。 它有很多用途,可以让您围绕它快速开发应用程序。 下面是一个例子:

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);
String cacheName = "demoCache";
String counterName = "DistributedCounter:Customers";

// Initialize an instance of the cache to begin performing operations:
Cache cache = CacheManager.getCache(cacheName);

Counter counter = cache.getDataStructuresManager().createCounter(counterName);
counter.setValue(1000);
long newValue = counter.incrementBy(5);
newValue = counter.getValue();
newValue = counter.decrementBy(2);

接下来做什么?

NCache 配套文档
版本比较
请求个性化的现场演示
下载 NCache

联系我们

联系电话
©版权所有 Alachisoft 2002 - 版权所有。 NCache 是 Diyatech Corp. 的注册商标。