の分散データ構造 NCache

NCache 非常に高速でスケーラブルな分散キャッシュです。 .NET, Java, Node.js, Python 分野の様々なアプリケーションで使用されています。 NCache 高トランザクションの.NETサーバーアプリケーションでアプリケーションデータのキャッシュに使用されます。 ASP.NET / ASP.NETコア セッションストレージ、および Pub / Subメッセージング.

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

ご覧のとおり、「Monday」を 2 回目に追加しても、HashSet には追加されません。

 

分散辞書

A 分散辞書 (IDistributedDictionary) は、通常の .NET IDictionary インターフェイスの代替として機能しますが、複数のアプリケーションやユーザーで共有できます。Add、Update、Remove、Contains など、Dictionary のすべての操作を提供します。

通常の辞書機能に加えて、分散辞書は、以下に基づいて項目を期限切れにする機能を提供します。 NCache のような有効期限オプション 絶対有効期限 (NAIST) と スライド式の有効期限.

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

次はどうする?

よくある質問(FAQ)

Yes. NCache クラスター全体にわたってスレッドの安全性を保証します。内部分散ロックを使用して同時実行性を管理し、複数のアプリケーションからの同時更新によってデータ破損や競合状態が発生しないようにします。

分散辞書はプロセスやサーバー間で共有されますが、標準辞書は単一のアプリケーションのローカルメモリにのみ存在します。さらに、 NCache 標準の .NET コレクションにはない機能である、データの有効期限 (絶対およびスライディング) とレプリケーションによる高可用性をサポートします。

データは引き続き利用可能です。構造は外部にホストされているため、 NCache クラスタ(アウトオブプロセス)であるため、アプリケーションのライフサイクルから独立しています。データはアプリケーションの再起動、クラッシュ、デプロイメント後も保持されます。

最小限のリファクタリングが必要です。 NCache 次のような標準.NETインターフェースを実装します IDictionary<K,V> (NAIST) と IList<T>標準的な方法( 追加、削除、含む)を使用してオブジェクトの初期化を置き換えるだけです。 NCache ハンドル。

お問い合わせ

電話

+1 214-619-2601 (米国)

+44 20 7993 8327 (英国)

©著作権 Alachisoft 2002 - . All rights reserved. NCache はダイヤテック株式会社の登録商標です。