캐시에서 JSON 데이터 쿼리
쿼리 인덱스(아래 단계에서 자세히 설명)를 사용하거나 이와 관련된 검색 가능한 속성을 사용하여 Json 데이터를 쿼리할 수 있습니다. 검색 가능한 속성은 다음과 같습니다.
- 태그
- 명명된 태그
- 그룹
주의 사항
이 기능은 다음에서도 사용할 수 있습니다. NCache Professional.
캐시에서 JSON 데이터를 쿼리하기 위한 전제 조건
- 모든 작업에 필요한 표준 전제 조건에 대해 알아보려면 NCache JSON 데이터를 포함한 클라이언트측 기능은 다음 페이지를 참조하세요. 클라이언트 측 API 전제 조건.
- 인덱스를 사용하여 데이터를 쿼리하려면 관리자 가이드.
- 인덱스를 정의한 경우 생성 시 인덱스할 JSON 개체에 대한 유형 정보를 제공해야 합니다.
- API 세부 정보는 다음을 참조하세요. 아이캐시, 캐시 아이템, 제이슨값, JsonObject, 그룹, 태그, 명명된 태그, 끼워 넣다, 속성 추가, 돈을 받아가세요, 쿼리 명령, 검색 서비스, ExecuteReader, 필드카운트, NamedTags사전.
1단계: 쿼리를 위한 JSON 데이터 인덱스 정의
인덱스를 사용하여 데이터를 쿼리하려면 관리자 가이드 를 사용하여 NCache 관리 센터 또는 명령줄 도구. 유형 이름이나 클래스를 지정해야 하는 경우(예: Alachisoft.NCache.Customer)와 나중에 쿼리할 속성(예: CompanyName)이 포함됩니다.
2단계: JSON 인덱스 쿼리에 대한 유형 정보 지정
쿼리하려면 유형 이름(이전에 정의한 인덱스와 동일한 유형 이름)을 제공해야 합니다. JsonObject
이 개체를 캐시에 추가할 때. 그렇지 않으면 쿼리를 성공적으로 수행할 수 없습니다. 다음 섹션에서 코드 조각에 이러한 유형의 정보를 지정하는 방법을 알아볼 수 있습니다. 유형 이름을 사용하여 캐시에 JsonObject 추가
3단계: JSON 쿼리 인덱스를 사용하여 JsonObject 쿼리
이전 두 단계를 완료한 경우 아래 코드 조각에 설명된 대로 필요한 속성(예: CompanyName)에 따라 검색할 수 있습니다. 캐시에서 검색된 개체의 적절한 역직렬화 및 유효성 검사를 보장하려면 유형 정보를 추가해야 합니다.
// Preconditions:
// (1) Cache is already connected
// (2) Cache is JSON Serialized
// Retrieve the CacheItem using the CompanyName with a query
string query = "SELECT * FROM Alachisoft.NCache.Customer WHERE $CompanyName$ = ?";
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$CompanyName$", "CustomerItem");
var queryResult = cache.SearchService.ExecuteReader(queryCommand);
// QueryResult contains all the keys and metadata of result
if (queryResult.FieldCount > 0)
{
while (queryResult.Read())
{
// Perform operation according to your logic
}
}
else
{
// No data containing the given CompanyName exist
}
주의 사항
작업이 안전하도록 하려면 에 설명된 대로 응용 프로그램 내에서 잠재적인 예외를 처리하는 것이 좋습니다. 처리 실패.
그룹, 태그 또는 명명된 태그를 사용하여 JsonObject 쿼리
당신은 추가 할 수 있습니다 JsonObject
캐시에 CacheItem
. CacheItem
에서 제공하는 사용자 정의 클래스입니다. NCache 캐시에 데이터를 추가하는 데 사용할 수 있습니다. CacheItem을 사용하면 다양한 속성을 추가할 수 있습니다. 예: 잠금, 의존, 태그, 명명된 태그및 그룹.
경고
속성은 개체 자체에 대한 참조를 포함할 수 없습니다. 그렇게 하면 예외가 발생합니다.
다음 예는 다음을 추가합니다. CacheItem
캐시에 JsonObject
빠른 의 가치로 CacheItem
. 태그가 추가됩니다. CacheItem
그런 다음 쿼리 명령을 사용하여 태그를 사용하여 항목을 가져옵니다.
Customer customer = FetchCustomerFromDB(1001);
// Create a unique key for the object
string key = $"Customer:{customer.CustomerID}";
// Create a new JSON object and set attributes
// string values need to be added with JsonValue
var jsonCustomer = new JsonObject("Alachisoft.NCache.Customer");
jsonCustomer.AddAttribute("CustomerID", customer.CustomerID);
jsonCustomer.AddAttribute("ContactName", (JsonValue)customer.ContactName);
jsonCustomer.AddAttribute("CompanyName", (JsonValue)customer.CompanyName);
jsonCustomer.AddAttribute("Phone", customer.Phone);
jsonCustomer.AddAttribute("Address", customer.Address);
// Create a new CacheItem
var item = new CacheItem(jsonCustomer);
// Add Tag to the cacheItem
item.Tags = new Tag[] { new Tag("CustomerItem") };
// Add CacheItem in the cache with the value
cache.Insert(key, item);
// Retrieve the CacheItem using the tag with a query
string query = "SELECT * FROM Alachisoft.NCache.Customer WHERE $Tag$ = ?";
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Tag$", "CustomerItem");
var queryResult = cache.SearchService.ExecuteReader(queryCommand);
// QueryResult contains all the keys and metadata of result
if (queryResult.FieldCount > 0)
{
while (queryResult.Read())
{
// Perform operation according to your logic
}
}
else
{
// No data containing the named tag(s) exist
}
추가 자료
NCache 에서 JSON으로 데이터 캐시에 대한 샘플 애플리케이션 제공 GitHub의.
도 참조
.그물: Alachisoft.NCache.런타임.JSON 네임 스페이스.
자바 : COM.alachisoft.ncache.runtime.json 네임 스페이스.