使用 SQL 查询在缓存中搜索组数据
NCache 提供对象查询,您可以通过它使用查询中给定的条件来搜索结果集。 要根据您指定的条件检索数据, NCache 为您提供 SQL 的扩展。
一个特殊的关键字 $Group$
用于指定所考虑的条件使用组。 使用搜索条件执行查询 ExecuteReader
.
ExecuteReader
在服务器端处理查询,然后以表格形式将结果以块的形式(作为包含键和值的字典)发送到客户端 ICacheReader
实例类型。
使用 SQL 查询在缓存中搜索组数据的先决条件
警告
如果搜索到的组不在缓存中,则查询将作为 Null
计算值。
使用 SQL 查询分组搜索数据
您还可以通过 SQL 搜索分组中存在的缓存项。 以下程序通过 SQL 查询搜索组中存在的客户 ID。
// A user wants to see all the West Coast Customers using the SQL query.
// Preconditions: Cache is already connected
// Items with groups exist in the cache
// 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 (FQN)
string query = "SELECT CustomerID,ContactName FROM Alachisoft.NCache.Samples.Data.Customer WHERE $Group$ = ? ";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.Parameters.Add("$Group$", "West Coast Customers");
// Executing the 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 customerName = reader.GetValue<string>("ContactName");
Console.WriteLine($"Customer '{customerName}' having ID '{customerID}' belongs to West Coast.");
}
}
else
{
Console.WriteLine($"No customers from West Coast found");
}
// A user wants to see all the West Coast Customers using the SQL query.
// Preconditions: Cache is already connected
// Items with groups exist in the cache
// 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 (FQN)
String query = "SELECT customerID,contactName FROM com.alachisoft.ncache.samples.Customer WHERE $Group$ = ?";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.getParameters().put("$Group$", "West Coast Customers");
// Executing the 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 = null;
customerID = reader.getValue("customerID", String.class);
String customerName = null;
customerName = reader.getValue("contactName", String.class);
System.out.println("Customer '" + customerName + "' having ID '" + customerID + "' belongs to West Coast.");
}
}
else {
System.out.println("No customers from West Coast found");
}
// This is an async method
// Preconditions: Cache is already connected
// Items with groups exist in cache
// Custom class is query indexed through the NCache Management Center or config.ncconf
// Search for items with group
// Provide Fully Qualified Name (FQN) of your custom class
var query = "SELECT $Group$ FROM FQN.Customer WHERE CustomerName = ?";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
// Providing parameters for query
var parameters = this.map.set("$Group","Important Customers");
queryCommand.setParameters(parameters);
// Executing QueryCommand
var searchService = await this.cache.getSearchService();
var cacheReader = await searchService.executeReader(queryCommand);
// Check if the result set is not empty
if (cacheReader.getFieldCount() > 0)
{
while (cacheReader.read())
{
// Perform operations
var groupName = cacheReader.getValue(1);
}
}
else
{
// Null query result set retrieved
}
# Preconditions: Cache is already connected
# Items with groups exist in cache
# Custom class is query indexed through the the NCache Management Center or config.ncconf
# Search for items with group
# Provide Fully Qualified Name(FQN) of your custom class
query = "SELECT $Group$ FROM FQN.Customer WHERE customer_name = ?"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
# Providing parameters for query
parameters = {"$Group": "Important Customers"}
query_command.set_parameters(parameters)
# Executing QueryCommand
search_service = cache.get_search_service()
cache_reader = search_service.execute_reader(query_command)
# Check if the result set is not empty
if cache_reader.get_field_count() > 0:
while cache_reader.read():
# Perform operations
group_name = cache_reader.get_value(str, 1)
else:
# None query result set retrieved
print("Query result is None")
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
更多资讯
NCache 为 Groups on 提供示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache。客户 命名空间。
Java的: COM。alachisoft.ncache。客户 命名空间。
节点.js: 缓存 类。
Python: ncache。客户 类。