캐시 사용량을 통해 읽기
이 섹션에서는 Read-Through 캐시 공급자를 구성하고 배포한 후 사용하는 방법을 설명합니다. NCache 제공 Alachisoft.NCache.Runtime.Caching.ReadThruOptions
API에서 Read-Thru 옵션을 지정하는 클래스입니다. 그만큼 ReadThruOptions
클래스에는 열거형이 포함되어 있습니다. ReadMode
, 다음 중 하나일 수 있습니다. ReadThru
or ReadThruForced
, 아래에 설명되어 있습니다.
ReadThru
캐시에 항목이 있는지 확인하고 항목이 없으면 데이터 소스에서 데이터를 가져옵니다.ReadThruForced
캐시에 항목이 있는지 여부에 관계없이 데이터 소스에서 항목을 가져옵니다.
여러 Read-Through 캐시 공급자는 다음을 통해 구성할 수 있습니다. NCache. API를 통해 특정 공급자 이름이 언급되지 않으면 기본 Read-Through 캐시 공급자가 호출됩니다. 공급자별 API 오버로드를 사용하여 기본 공급자 이외의 공급자를 사용할 수도 있습니다.
중대한
Java의 경우 JAR 파일을 배포하기 전에 다음을 확인해야 합니다.
- JDK 17 설치되어 있습니다.
- Java용 환경 변수가 설정되었습니다.
캐시를 통해 읽기를 사용하기 위한 전제 조건
- 모든 작업에 필요한 표준 전제 조건에 대해 알아보려면 NCache 서버 측 기능은 다음 페이지를 참조하십시오. 서버 측 API 전제 조건.
- API 세부 정보는 다음을 참조하세요. 캐시 아이템, 돈을 받아가세요, 읽기 모드, 대량 가져오기, 캐시 연결 옵션, ReadThru 옵션.
Get 작업과 함께 Read-Through 사용
다음 예제는 다음을 사용하여 지정된 키에 해당하는 Read-Through가 활성화된 항목을 검색합니다. Get
방법.
// Pre-condition: Cache is already connected
// Specify the key of the item
string key = $"Product:{product.ProductID}";
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThru;
// Retrieve the data of the corresponding item with read thru enabled
Product data = cache.Get<Product>(key, readThruOptions);
if (data != null)
{
// Perform operations accordingly
}
주의 사항
작업이 안전하도록 하려면 에 설명된 대로 응용 프로그램 내에서 잠재적인 예외를 처리하는 것이 좋습니다. 처리 실패.
ForcedRead-Through 사용
NCache 강제 Read-Through 옵션을 제공합니다. NCache 데이터 소스에서 데이터를 강제로 가져옵니다. 이는 데이터가 캐시에서 확인되지 않고 데이터 소스에서 직접 가져옴을 의미합니다. 다음을 지정하여 강제 읽기를 활성화할 수 있습니다. ReadMode
as ReadThruForced
.
다음 예제는 다음을 사용하여 캐시에서 항목을 가져옵니다. ReadThruForced
Read-Through를 강제로 활성화하여 옵션을 선택합니다.
// Specify the key of the item
string key = $"Product:{product.ProductID}";
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThruForced;
// Retrieve the data of the corresponding item with read thru enabled
Product data = cache.Get<Product>(key, readThruOptions);
if (data != null)
{
// Perform operations accordingly
}
대량 작업에 Read-Through 사용
다음 예는 다음을 사용하여 지정된 키에 해당하는 Read-Through가 활성화된 제품 사전을 검색합니다. GetBulk
방법.
팁
이러한 작업을 더 잘 이해하려면 다음을 검토하세요. 대량 작업.
// Create a new array of keys
String[] keys = new string[];
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThru;
// Retrieve the dictionary of Products with corresponding products
IDictionary<string, Product> retrievedItems = cache.GetBulk<Product>(keys, readThruOptions);
// IDictionary contains cached keys and values
CacheItem과 함께 Read-Through 사용
다음 예제는 CacheItem
지정된 키에 해당하는 Read-Through 활성화.
// Specify the key of the item
string key = $"Product:{product.ProductID}";
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThru;
// Retrieve the data of the corresponding item with read thru enabled
CacheItem data = cache.GetCacheItem(key, readThruOptions);
if (data != null)
{
// Perform operations accordingly
}
데이터 구조와 함께 Read-Through 사용
다음 예에서는 지정된 키에 해당하는 Read-Through가 활성화된 Counter, List, Queue, HashSet 및 Dictionary를 검색합니다.
// Specify the key of the item
string key = $"Product:{product.ProductID}";
// Specify the readThruOptions for read through operations
var readThruOptions = new ReadThruOptions();
readThruOptions.Mode = ReadMode.ReadThru;
switch(mainMenu)
{
case mainMenu.GetDistributedCounter:
// Retrieve the count of the corresponding item with read thru enabled
ICounter counter = cache.DataTypeManager.GetCounter(key, readThruOptions);
break;
case mainMenu.GetDistributedDictionary:
// Retrieve dictionary of the corresponding item with read thru enabled
IDistributedDictionary<string, object> distributedDictionary = cache.DataTypeManager.GetDictionary<string, object>(key,readThruOptions);
break;
case mainMenu.GetDistributedList:
// Retrieve the list of the corresponding item with read thru enabled
IDistributedList<object> distributedList = cache.DataTypeManager.GetList<object>(key, readThruOptions);
break;
case mainMenu.GetDistributedQueue:
// Retrieve the queue of the corresponding item with read thru enabled
IDistributedQueue<object> distributedQueue = cache.DataTypeManager.GetQueue<object>(key, readThruOptions);
break;
case mainMenu.GetDistributedHashSet:
// Retrieve the HashSet of the corresponding item with read thru enabled
IDistributedHashSet<int> distributedHashSet = cache.DataTypeManager.GetHashSet<int>(key, readThruOptions);
break;
}
기본 Read-Through 공급자 구성
다음을 통해 기본 공급자를 지정할 수 있습니다. NCache 관리센터나 클라이언트.ncconf 파일. 공급자 이름이 API와 클라이언트.ncconf이면 기본 공급자가 자동으로 사용됩니다.
<cache id="demoCache" default-readthru-provider="defaultProviderName" client-cache-id="" client-cache-syncmode="optimistic" default-writethru-provider="" load-balance="True">
...
</cache>
CacheConnectionOptions
공급자를 지정하는 데 사용할 수도 있습니다. NCache 제공된 어셈블리를 로드하는 동안 예외가 발생할 경우 캐시 로그에 오류를 제공합니다.
추가 자료
NCache Read-Through에 대한 샘플 애플리케이션을 제공합니다. GitHub의.
도 참조
.그물: Alachisoft.NCache.실행 시간 네임 스페이스.
자바 : COM.alachisoft.ncache.runtime.datasourceprovider 네임 스페이스.