使用 SQL 查询搜索缓存中的标签数据
NCache 提供对象查询,您可以通过这些查询根据提供给查询的条件搜索和删除结果集。 要根据您指定的标准检索数据, NCache 为您提供 SQL 查询的扩展。它允许您根据要求的条件搜索缓存中的数据。
一个特殊的关键字 $Tag$
用于指定所考虑的条件使用标签。使用搜索条件执行 SQL 查询 ExecuteReader
.
ExecuteReader
在服务器端处理查询,然后以表格形式将结果以块的形式(作为包含键和值的字典)发送到客户端 ICacheReader
实例类型。
您可以查询具有特定标签的项目,如下所述。
使用 SQL 查询在缓存中搜索标签数据的先决条件
使用一个标签在缓存中搜索数据
使用对象查询,单个标签可用于检索与该标签关联的所有项目。
句法
以下示例检索与该标签关联的所有项目 贵宾客户 使用 SQL 查询。
备注
使用类 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 部分。
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
更多资讯
NCache 提供标签的示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache.运行时.缓存 命名空间。
Java的: COM。alachisoft.ncache.runtime.caching 命名空间。
节点.js: 标签 类。
Python: ncache.runtime.caching 类。