オブジェクト クエリを使用して、クエリ条件を名前付きタグとしてキャッシュからアイテムを取得することもできます。 クエリは実行されると、SQL クエリで指定された名前付きタグに従ってキャッシュを検索します。 検索する前に、項目が名前付きタグとともにキャッシュに追加されていることを確認してください。 を参照してください。 名前付きタグ付きのアイテムを追加 名前付きタグの作成と追加の詳細については、セクションを参照してください。
前提条件
次の例では、名前付きタグの値が指定された値と一致する製品をキャッシュから取得します。
// Preconditions: Cache is already connected
// User wants to see the details of all the customers having 12% VIP membership discount using SQL query.
// Custom class is query indexed through the NCache Management Center or config.ncconf*/
// Create an SQL Query with the specified criteria
// Make sure to use the Fully Qualified Name for custom class
string query = "SELECT CustomerID,ContactName FROM Alachisoft.NCache.Samples.Data.Customer WHERE VIP_Membership_Discount = 0.12 ";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Executing the Query
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Read results if the 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 customerName = reader.GetValue<string>("ContactName");
Console.WriteLine($"Customer '{customerName}' with ID '{customerID}' has VIP membership discount.");
}
}
else
{
Console.WriteLine($"No VIP members found");
}
// Preconditions: Cache is already connected
// User wants to see the details of all the customers having 12% VIP membership discount using SQL query.
// Custom class is query indexed through the NCache Management Center or config.ncconf*/
// Create an SQL Query with the specified criteria
// Make sure to use the Fully Qualified Name for custom class
// Create an SQL Query with the specified criteria
String query = "SELECT customerID,contactName FROM com.alachisoft.ncache.sample.Customer WHERE VIP_Membership_Discount = 0.12";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
// Executing the Query
CacheReader reader = cache.getSearchService().executeReader(queryCommand);
// Read results if the 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 customerName = reader.getValue("contactName", String.class);
System.out.println("Customer '" + customerName + "' with ID '" + customerID + "' has VIP membership discount.");
}
}
else
{
System.out.println("No VIP members found");
}
// This is an async method
// Preconditions: Cache is already connected
// Items are already present in the cache with Named Tags
// Custom class is query indexed through the NCache Management Center or config.ncconf
// Create SQL Query with the specified criteria
// Make sure to use the Fully Qualified Name (FQN)
var query = "SELECT $Value$ FROM FQN.Product WHERE FlashSaleDiscount = ?";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
let parameter = new Map();
parameter.set("FlashSaleDiscount", 0.5);
queryCommand.setParameters(parameter);
// 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, ncache.JsonDataType.Object);
}
}
else
{
// Null query result set retrieved
}
# Preconditions: Cache is already connected
# Items are already present in the cache with Named Tags
# Custom class is query indexed through the NCache Management Center or config.ncconf
# Create SQL Query with the specified criteria
# Make sure to use the Fully Qualified Name(FQN)
query = "SELECT $Value$ FROM FQN.Product WHERE FlashSaleDiscount = ?"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
parameter = {"FlashSaleDiscount": 0.5}
query_command.set_parameters(parameter)
# 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(Product, 1)
else:
# None query result set retrieved
print("Query result is None")
Note
操作がフェイルセーフであることを保証するために、で説明されているように、アプリケーション内の潜在的な例外を処理することをお勧めします。 失敗の処理.
警告
同じキャッシュを共有する複数のアプリケーションがあり、それらのすべてが名前付きタグを追加することになっている場合は、同じ名前付きタグのデータ型が同種であることを確認してください。 たとえば、XNUMX つのクライアントが名前付きタグを追加しているとします。 商品ID 文字列データ型の場合、他のすべてのクライアントは次の値を追加する必要があります。 商品ID 同じキャッシュの文字列形式のみ。
オブジェクト クエリの詳細については、を参照してください。 のSQLリファレンス NCache のセクションから無料でダウンロードできます。
その他のリソース
NCache タグのサンプル アプリケーションを提供します。 GitHubの.
も参照してください
。ネット: Alachisoft.NCache.ランタイム.キャッシュ 名前空間
Java: comの。alachisoft.ncache.ランタイムキャッシュ 名前空間
Node.js: 名前付きタグ辞書 とに提供されます。
Python: ncache.ランタイムキャッシュ とに提供されます。