SQLクエリを使用してキャッシュ内のタグデータを検索する
NCache クエリに指定された基準に基づいて結果セットを検索および削除できるオブジェクトクエリを提供します。 指定した基準に従ってデータを取得するには、 NCache SQL クエリの拡張機能を提供します。要件の基準に基づいてキャッシュ内のデータを検索できます。
特別なキーワード $Tag$
は、検討中の条件がタグを使用することを指定するために使用されます。検索条件を含む SQL クエリは次のように実行されます。 ExecuteReader
.
ExecuteReader
サーバー側でクエリを処理し、結果をチャンクで (キーと値を含む辞書として) クライアントに表形式で送信します。 ICacheReader
インスタンスのタイプ。
以下で説明するように、特定のタグを持つアイテムを照会できます。
SQL クエリを使用してキャッシュ内のタグ データを検索するための前提条件
XNUMXつのタグでキャッシュ内のデータを検索
オブジェクトクエリを使用すると、単一のタグを使用して、そのタグに関連付けられているすべてのアイテムを取得できます。
構文
次の例では、タグに関連付けられたすべての項目を取得します。 VIPのお客様 SQLクエリを使用します。
Note
Customer クラスの完全修飾名 (FQN) を使用します (例: Data.Customer)。
// Preconditions: Cache is already connected
// Create a query for search
// Use the Fully Qualified Name (FQN) of your own custom class
string query = "Select CustomerID,ContactName FROM Alachisoft.NCache.Samples.Data.Customer WHERE $Tag$ = ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Tag$", "VIP Customers");
// Executing Query
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Read results if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
string customerID = reader.GetValue<string>("CustomerID");
string contactName = reader.GetValue<string>("ContactName");
Console.WriteLine($"Customer '{contactName}' having ID '{customerID}' is a VIP Customer.");
}
}
else
{
Console.WriteLine($"No VIP Customers found");
}
// Pre-conditions: Cache is already connected
// Create a query for search
// Use the Fully Qualified Name (FQN) of your custom class
String query = "Select customerID,contactName FROM com.alachisoft.ncache.sample.Customer WHERE $Tag$ = ?";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
queryCommand.getParameters().put("$Tag$", "VIP Customers");
// Executing Query
CacheReader reader = cache.getSearchService().executeReader(queryCommand);
// Read results if result set is not empty
if (reader.getFieldCount() > 0) {
while (reader.read()) {
// Get the value of the result set
String customerID = reader.getValue("customerID", String.class);
String contactName = reader.getValue("contactName", String.class);
System.out.println("Customer '" + contactName + "' having ID '" + customerID + "' is a VIP Customer.");
}
} else {
System.out.println("No VIP Customers found");
}
// This is an async method
// Preconditions: Cache is already connected
// Items are already present in the cache with tags
// Custom class is query indexed through NCache Web Manager or config.ncconf
// Search for items with tags
// Use the Fully Qualified Name (FQN) of your own custom class
var query = "Select $Value$ FROM FQN.Customer WHERE $Tag$ = ?";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
queryCommand.getParameters().set("$Tag$", "Important Customers");
// Executing query
var searchService = await this.cache.getSearchService();
var reader = await searchService.executeReader(queryCommand);
// Check if result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
// Get the value of the result set
var result = reader.getValue(1);
}
}
else
{
// Null query result set retrieved
}
# Preconditions: Cache is already connected
# Items are already present in the cache with tags
# Custom class is query indexed through NCache Web Manager or config.ncconf
# Search for items with tags
# Use the Fully Qualified Name(FQN) of your own custom class
query = "Select $Value$ FROM FQN.Customer WHERE $Tag$ = ?"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
query_command.set_parameters({"$Tag$": "Important Customers"})
# Executing query
search_service = cache.get_search_service()
reader = search_service.execute_reader(query_command)
# Check if result set is not empty
if reader.get_field_count() > 0:
while reader.read():
# Get the value of the result set
result = reader.get_value(Customer, 1)
else:
# None query result set retrieved
print("Result set is None")
警告
提供する Null
クエリのタグ値は、ArgumentNullExceptionまたはNullPointerExceptionをスローします。
詳細を取得するために NCache クエリは、を参照してください のSQLリファレンス NCache のセクションから無料でダウンロードできます。
Note
操作がフェイルセーフであることを保証するために、で説明されているように、アプリケーション内の潜在的な例外を処理することをお勧めします。 失敗の処理.
その他のリソース
NCache タグのサンプルアプリケーションを提供します GitHubの.
も参照してください
。ネット: Alachisoft.NCache.ランタイム.キャッシュ 名前空間
Java: comの。alachisoft.ncache.ランタイムキャッシュ 名前空間
Node.js: タグ とに提供されます。
Python: ncache.ランタイムキャッシュ とに提供されます。