对象查询还可用于从缓存中检索项目,查询条件为命名标签。 执行查询时将根据 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")
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
警告
如果您有多个共享相同缓存的应用程序,并且所有应用程序都应该添加命名标签,那么请确保相同的命名标签具有同质的数据类型。 例如,如果一个客户端正在添加命名标签 产品编号 使用字符串数据类型,那么所有其他客户端应添加以下值 产品编号 仅以字符串格式用于相同的缓存。
要获得有关对象查询的更多详细信息,请参阅 SQL 参考 NCache 部分。
更多资讯
NCache 提供了标签的示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache.运行时.缓存 命名空间。
Java的: COM。alachisoft.ncache.runtime.caching 命名空间。
节点.js: 命名标签字典 类。
Python: ncache.runtime.caching 类。