Leggere l'utilizzo della cache
Questa sezione spiega l'utilizzo del provider della cache Read-Through dopo averlo configurato e distribuito. NCache fornisce il Alachisoft.NCache.Runtime.Caching.ReadThruOptions
classe per specificare le opzioni Read-Thru nell'API. IL ReadThruOptions
la classe contiene enum ReadMode
, che può essere ReadThru
or ReadThruForced
, spiegato di seguito.
ReadThru
verifica l'esistenza di un elemento nella cache e, se non lo trova, recupera i dati dall'origine dati.ReadThruForced
recupera l'elemento da un'origine dati, indipendentemente dalla sua esistenza nella cache o meno.
È possibile configurare più provider di cache Read-Through NCache. Il provider della cache Read-Through predefinito verrà chiamato se il nome di un provider specifico non viene menzionato tramite l'API. È inoltre possibile utilizzare provider diversi da quello predefinito utilizzando gli sovraccarichi dell'API specifici del provider.
Consigli
Per Java, prima di distribuire i tuoi file JAR, devi assicurarti che:
- JDK 17 è installato.
- La variabile di ambiente per Java è impostata.
Prerequisiti per utilizzare Read Through Cache
- Per conoscere i prerequisiti standard richiesti per lavorare con all NCache caratteristiche lato server si prega di fare riferimento alla pagina indicata su Prerequisiti API lato server.
- Per i dettagli dell'API fare riferimento a: CacheItem, Ottieni, Modalità di lettura, Ottieni Bulk, Opzioni di connessione cache, Leggi attraverso Opzioni.
Utilizzo della lettura continua con l'operazione Get
L'esempio seguente recupera un elemento con Read-Through abilitato, corrispondente alla chiave specificata utilizzando l'estensione Get
metodo.
// Pre-condition: Cache is already connected
// Specify the key of the item
string key = $"Product:{product.ProductID}";
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThru;
// Retrieve the data of the corresponding item with read thru enabled
Product data = cache.Get<Product>(key, readThruOptions);
if (data != null)
{
// Perform operations accordingly
}
Note:
Per garantire che l'operazione sia a prova di errore, si consiglia di gestire eventuali potenziali eccezioni all'interno dell'applicazione, come spiegato in Gestione dei guasti.
Utilizzo della lettura forzata
NCache fornisce l'opzione di Read-Through forzato, attraverso il quale NCache recupera forzatamente i dati dall'origine dati. Ciò significa che i dati non verranno controllati nella cache e verranno recuperati direttamente dall'origine dati. È possibile abilitare il Read-Through forzato specificando il file ReadMode
as ReadThruForced
.
L'esempio seguente ottiene un elemento dalla cache utilizzando il file ReadThruForced
opzione abilitando forzatamente il Read-Through su di esso.
// Specify the key of the item
string key = $"Product:{product.ProductID}";
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThruForced;
// Retrieve the data of the corresponding item with read thru enabled
Product data = cache.Get<Product>(key, readThruOptions);
if (data != null)
{
// Perform operations accordingly
}
Utilizzo del read-through con operazioni in blocco
Nell'esempio seguente viene recuperato un dizionario di prodotti con Read-Through abilitato, corrispondenti alle chiavi specificate utilizzando l'estensione GetBulk
metodo.
Consiglio
Per una migliore comprensione di queste operazioni rivedere Operazioni di massa.
// Create a new array of keys
String[] keys = new string[];
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThru;
// Retrieve the dictionary of Products with corresponding products
IDictionary<string, Product> retrievedItems = cache.GetBulk<Product>(keys, readThruOptions);
// IDictionary contains cached keys and values
Utilizzo della lettura continua con CacheItem
L'esempio seguente recupera a CacheItem
con Read-Through abilitato, corrispondente alla chiave specificata.
// Specify the key of the item
string key = $"Product:{product.ProductID}";
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThru;
// Retrieve the data of the corresponding item with read thru enabled
CacheItem data = cache.GetCacheItem(key, readThruOptions);
if (data != null)
{
// Perform operations accordingly
}
Utilizzo del read-through con le strutture di dati
L'esempio seguente recupera un contatore, un elenco, una coda, un HashSet e un dizionario con Read-Through abilitato, corrispondente alla chiave specificata.
// Specify the key of the item
string key = $"Product:{product.ProductID}";
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThru;
switch(mainMenu)
{
case mainMenu.GetDistributedCounter:
// Retrieve the count of the corresponding item with read thru enabled
ICounter counter = cache.DataTypeManager.GetCounter(key, readThruOptions);
break;
case mainMenu.GetDistributedDictionary:
// Retrieve dictionary of the corresponding item with read thru enabled
IDistributedDictionary<string, object> distributedDictionary = cache.DataTypeManager.GetDictionary<string, object>(key,readThruOptions);
break;
case mainMenu.GetDistributedList:
// Retrieve the list of the corresponding item with read thru enabled
IDistributedList<object> distributedList = cache.DataTypeManager.GetList<object>(key, readThruOptions);
break;
case mainMenu.GetDistributedQueue:
// Retrieve the queue of the corresponding item with read thru enabled
IDistributedQueue<object> distributedQueue = cache.DataTypeManager.GetQueue<object>(key, readThruOptions);
break;
case mainMenu.GetDistributedHashSet:
// Retrieve the HashSet of the corresponding item with read thru enabled
IDistributedHashSet<int> distributedHashSet = cache.DataTypeManager.GetHashSet<int>(key, readThruOptions);
break;
}
Configura provider read-through predefinito
È possibile specificare il provider predefinito tramite il file NCache Centro Gestione o tramite il client.ncconf file. Se il nome del provider non viene fornito sia nell'API che in client.ncconf, verrà utilizzato automaticamente il provider predefinito.
<cache id="demoCache" default-readthru-provider="defaultProviderName" client-cache-id="" client-cache-syncmode="optimistic" default-writethru-provider="" load-balance="True">
...
</cache>
CacheConnectionOptions
può essere utilizzato anche per specificare i provider. NCache fornisce errori nei log della cache in caso di eccezione durante il caricamento degli assembly forniti.
Risorse addizionali
NCache fornisce un'applicazione di esempio per Read-Through GitHub.
Vedere anche
.NETTO: Alachisoft.NCache.Durata spazio dei nomi.
Giava: com.alachisoft.ncache.runtime.datasourceprovider spazio dei nomi.