개체 쿼리는 명명된 태그인 쿼리 기준을 사용하여 캐시에서 항목을 검색하는 데에도 사용할 수 있습니다. 쿼리가 실행되면 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")
주의 사항
작업이 안전하도록 하려면 에 설명된 대로 응용 프로그램 내에서 잠재적인 예외를 처리하는 것이 좋습니다. 처리 실패.
경고
동일한 캐시를 공유하는 여러 애플리케이션이 있고 모두 명명된 태그를 추가해야 하는 경우 동일한 명명된 태그가 동일한 데이터 유형을 가지고 있는지 확인하세요. 예를 들어 한 클라이언트가 명명된 태그를 추가하는 경우 ProductID 문자열 데이터 유형을 사용하는 경우 다른 모든 클라이언트는 다음 값을 추가해야 합니다. ProductID 동일한 캐시에 대한 문자열 형식으로만 가능합니다.
개체 쿼리에 대한 자세한 내용은 다음을 참조하세요. 에 대한 SQL 참조 NCache 안내
추가 자료
NCache 태그에 대한 샘플 애플리케이션을 제공합니다. GitHub의.
도 참조
.그물: Alachisoft.NCache.런타임.캐싱 네임 스페이스.
자바 : COM.alachisoft.ncache.런타임.캐싱 네임 스페이스.
Node.js : NamedTags사전 클래스입니다.
파이썬 : ncache.런타임.캐싱 클래스입니다.