Strutture di dati distribuiti in NCache

NCache è una cache distribuita estremamente veloce e scalabile per .NET/.NET Core, Java, Node.js e applicazioni Python. NCache viene utilizzato da applicazioni server .NET con transazioni elevate per la memorizzazione nella cache dei dati delle applicazioni, ASP.NET / ASP.NET Core archiviazione della sessione e messaggistica Pub/Sub.

NCache è diventato anche un archivio dati distribuito in memoria davvero potente condiviso tra più applicazioni .NET e Java. Di conseguenza, le applicazioni desiderano archiviare sempre più tipi di dati NCache da condividere tra più applicazioni. Per rispondere a questa esigenza, NCache fornisce strutture di dati distribuiti in Java e .NET da condividere tra più utenti. Sono:

Ciascuna di queste strutture di dati è descritta più dettagliatamente di seguito.

 

Coda distribuita

Una coda distribuita consente di creare una coda da condividere tra più applicazioni .NET e Java in esecuzione in un ambiente distribuito. Inoltre, la coda garantisce che la sequenza in cui hai inserito i dati venga mantenuta. Inoltre, tutte le applicazioni che recuperano i dati dalla coda possono fare affidamento su questa sequenza per essere sempre corretta.

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

Come puoi vedere sopra, una coda distribuita ti consente di aggiungere (accodare) elementi in una sequenza in modo da poterli rimuovere (eliminare dalla coda) in seguito nella stessa sequenza (FIFO). Ecco l'interfaccia della coda distribuita:

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

Come puoi vedere nell'interfaccia IDistributedQueue sopra menzionata, ti fornisce tutte le funzionalità tradizionali della coda.

 

HashSet distribuito

Un HashSet distribuito si comporta proprio come la classe .NET HashSet ma in modo condiviso per più applicazioni e utenti. Inoltre, un HashSet fornisce tutte le operazioni di impostazione come:

  • - Nessun duplicato: nel set
  • - Impostare le operazioni: come Unione, Intersezione e Differenza

Inoltre, essendo un HashSet condiviso per più applicazioni o utenti, diventa una potente struttura di dati da utilizzare. Ecco un esempio di come usarlo.

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

Come puoi vedere, quando aggiungi "Monday" la seconda volta, non viene aggiunto all'HashSet.

 

Dizionario distribuito

Un dizionario distribuito (IDistributedDictionary) si comporta come la normale interfaccia .NET IDictionary ma in modo condiviso per più applicazioni e utenti. Inoltre, un dizionario distribuito fornisce tutte le operazioni del dizionario come Aggiungi, Aggiorna, Rimuovi, Contiene e altro.

Oltre alle normali funzionalità del dizionario, il dizionario distribuito offre la possibilità di far scadere gli elementi in base a NCache opzioni di scadenza come Scadenza assoluta ed Scadenza scorrevole.

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

Come puoi vedere, il dizionario distribuito si comporta proprio come un normale distribuito .NET (interfaccia IDictionary). Ma, dietro le quinte, è distribuito, il che lo rende molto scalabile. Inoltre, è anche condiviso tra più applicazioni.

 

Elenco distribuito

L'elenco distribuito si comporta esattamente come il normale IList .NET, ma con la differenza che è distribuito e può essere condiviso tra più applicazioni e processi. L'elenco distribuito è un elenco non ordinato ed è possibile aggiungere elementi alla fine dell'elenco tramite il file Inserisci () metodo o in qualsiasi luogo attraverso il Inserire() metodo fornendo un indice.

Di seguito è riportato un esempio di come utilizzare un elenco distribuito.

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

Puoi anche rimuovere elementi dall'elenco, scorrere su di esso ed eseguire molte altre operazioni.

 

Contatore distribuito

Il contatore distribuito è una potente struttura di dati in NCache che consente di mantenere un contatore unico in un ambiente distribuito condiviso da più applicazioni. Questo ha molti usi e ti consente di sviluppare rapidamente applicazioni attorno ad esso. Di seguito è riportato un esempio:

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

Cosa fare dopo?

NCache Docs
Confronto edizione
Richiedi una Demo LIVE personalizzata
Scaricare NCache
© Copyright Alachisoft 2002 - . Tutti i diritti riservati. NCache è un marchio registrato di Diyatech Corp.