リードスルーキャッシュの使用量
このセクションでは、リードスルー キャッシュ プロバイダーを構成して展開した後の使用方法について説明します。 NCache 提供する Alachisoft.NCache.Runtime.Caching.ReadThruOptions
API で Read-Thru オプションを指定するクラス。 の ReadThruOptions
クラスには列挙型が含まれています ReadMode
、どちらかになります ReadThru
or ReadThruForced
、以下で説明します。
ReadThru
キャッシュ内に項目が存在するかどうかを確認し、見つからない場合はデータ ソースからデータをフェッチします。ReadThruForced
キャッシュ内にアイテムが存在するかどうかに関係なく、データ ソースからアイテムをフェッチします。
複数のリードスルー キャッシュ プロバイダーを設定できます。 NCache。 API を通じて特定のプロバイダー名が指定されていない場合は、デフォルトのリードスルー キャッシュ プロバイダーが呼び出されます。 API のプロバイダー固有のオーバーロードを使用して、デフォルト以外のプロバイダーを使用することもできます。
重要
Javaの場合、JARファイルをデプロイする前に、次のことを確認する必要があります。
- JDK 17 インストールされています。
- Javaの環境変数が設定されています。
リードスルーキャッシュを使用するための前提条件
- すべてを使用するために必要な標準的な前提条件について学習するには NCache サーバー側の機能については、指定されたページを参照してください サーバー側 API の前提条件.
- APIの詳細については、以下を参照してください。 キャッシュ項目, 入手, 読み取りモード, GetBulk, キャッシュ接続オプション, リードスルーオプション.
Get操作でリードスルーを使用する
次の例では、リードスルーが有効になっているアイテムを取得します。これは、 Get
方法。
// 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
操作がフェイルセーフであることを保証するために、で説明されているように、アプリケーション内の潜在的な例外を処理することをお勧めします。 失敗の処理.
ForcedRead-Throughを使用する
NCache 強制リードスルーのオプションを提供します。 NCache データソースからデータを強制的に取得します。 これは、データがキャッシュ内でチェックされず、データ ソースから直接フェッチされることを意味します。 強制リードスルーを有効にするには、 ReadMode
as ReadThruForced
.
次の例では、を使用してキャッシュからアイテムを取得します ReadThruForced
オプションでリードスルーを強制的に有効にします。
// 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
}
一括操作でのリードスルーの使用
次の例では、リードスルーが有効になっている製品の辞書を取得します。これは、 GetBulk
方法。
先端
これらの操作をより深く理解するには、次の手順を確認してください。 一括操作.
// 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
CacheItemでのリードスルーの使用
次の例では、 CacheItem
指定されたキーに対応するリードスルーが有効になっている。
// 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
}
データ構造でのリードスルーの使用
次の例では、指定されたキーに対応する、リードスルーが有効になっているカウンター、リスト、キュー、ハッシュセット、およびディクショナリを取得します。
// 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;
}
デフォルトのリードスループロバイダーを構成する
デフォルトのプロバイダーは、 NCache 管理センターまたは client.ncconf ファイル。 API と API の両方でプロバイダー名が指定されていない場合は、 client.ncconfの場合、デフォルトのプロバイダーが自動的に使用されます。
<cache id="demoCache" default-readthru-provider="defaultProviderName" client-cache-id="" client-cache-syncmode="optimistic" default-writethru-provider="" load-balance="True">
...
</cache>
CacheConnectionOptions
プロバイダーを指定するためにも使用できます。 NCache 提供されたアセンブリの読み込み中に例外が発生した場合、キャッシュ ログにエラーが記録されます。
その他のリソース
NCache Read-Through のサンプル アプリケーションを提供します。 GitHubの.
も参照してください
。ネット: Alachisoft.NCache。ランタイム 名前空間
Java: comの。alachisoft.ncache.runtime.datasourceprovider 名前空間