分布式数据结构 NCache

NCache 是一个极其快速且可扩展的分布式缓存 。NET, 爪哇岛, Node.jsPython 领域广泛应用,提供了卓越的解决方案。 NCache 被高事务 .NET 服务器应用程序用于应用程序数据缓存, ASP.NET / ASP.NET核心 会话存储,以及 发布/订阅消息.

NCache 提供 分布式数据结构 这些结构可供多个用户共享。与驻留在客户端应用程序堆中的本地结构不同,这些结构托管在分布式缓存集群中,从而减轻了本地内存压力和垃圾回收开销。这些结构包括:

特性 标准流程中收款 NCache 分布式结构
适用范围 进程密集型(本地堆) 集群范围(共享网络资源)
生命周期 应用程序重启/回收后丢失 与应用程序生命周期无关
可用性 单点故障(流程) 高可用性(通过分区/副本拓扑结构)
持续一致 仅在单个进程内线程安全 保证跨多个服务器的一致性

以下部分提供了 .NET 和 Java 应用程序的详细信息和代码示例。

 

分布式队列

A 分布式队列 实现 IDistributedQueue<T> 该接口允许您创建队列,以便在分布式环境中运行的多个 .NET 和 Java 应用程序之间共享。此外,该队列还能确保您输入数据的顺序得到维护。所有从队列中检索数据的应用程序都可以信赖此顺序的正确性。

string cacheName = "demoCache";
string QueueName = "DistributedQueue:Customers";
  
// Initialize an instance of the cache to begin performing operations
ICache cache = CacheManager.GetCache(cacheName);
  
// Create a thread-safe distributed queue visible to all cluster nodes
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);

// Create a thread-safe distributed queue visible to all cluster nodes
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();
      IList<T> DequeueBulk(int maxitems = 10);
      void Enqueue(T item);
      void EnqueueBulk(IList<T> items);
      T Peek();
      T[] ToArray();
  }
public interface DistributedQueue<T> extends Queue<T>, DistributedDataStructure, Notifiable {
    void clear();

    void copyTo(T[] var1, int var2);

    T peek();

    Object[] toArray();
}
 

分布式哈希集

A 分布式哈希集 其行为与 .NET HashSet 类类似,但以共享的方式供多个应用程序和用户使用。它提供以下所有 Set 操作:

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

作为多个应用程序或用户共享的 HashSet,它成为一个功能强大的数据结构。以下是如何使用它的一个示例。

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

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

// Creating a 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 a 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");

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

 

分布式字典

A 分布式字典 (IDistributedDictionary) 可作为常规 .NET IDictionary 接口的直接替代品,但以共享方式供多个应用程序和用户使用。它提供所有字典操作,例如添加、更新、删除、包含等。

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

ICache cache = 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 a distributed Dictionary with an 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");
 

分布式列表

分布式列表 完全实施 System.Collections.Generic.IList<T> 它提供了一个接口,使其可以作为标准列表的直接替代品。主要区别在于它是分布式的,可以在多个应用程序和进程之间共享。分布式列表是一个无序列表,您可以通过在列表末尾添加项目来扩展列表。 加() 方法或在任何位置通过 插入() 通过提供索引的方法。

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

ICache cache = 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 = 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 支持数据过期(绝对过期和滑动过期)和通过复制实现高可用性,这些功能在标准的 .NET 集合中并不存在。

数据仍然可用。由于这些结构托管在外部环境中 NCache 集群(进程外)数据独立于应用程序生命周期。即使应用程序重启、崩溃或部署,您的数据也不会丢失。

只需进行少量重构。 NCache 实现标准的 .NET 接口,例如 IDictionary<K,V>IList<T>您可以使用标准方法(例如 添加、删除、包含)并简单地替换对象初始化以使用 NCache 处理。

联系我们

联系电话

+1 214-619-2601 (美国)

+44 20 7993 8327 (英国)

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