Read-Through 캐싱 공급자 구성 및 구현
Read-Through 캐싱을 사용하려면 다음을 구현해야 합니다. IReadThruProvider
.NET용 인터페이스 및 ReadThruProvider
자바용 인터페이스. NCache 이 사용자 정의 구현을 사용하여 데이터 소스에서 데이터를 읽습니다. 참조하십시오 관리자 안내서의 읽기 구성 재구성 자세한 내용은.
경고
Read-Through 캐싱 공급자가 지정된 경우 클라이언트.ncconf FBI 증오 범죄 보고서 default-readthru-provider
태그 및 API를 통해서도 API의 제공자는 에 지정된 것을 덮어씁니다. 클라이언트.ncconf.
중대한
Java의 경우 Maven 종속성을 추가하기 전에 다음을 확인해야 합니다.
- JDK 17 설치되어 있습니다.
- Java용 환경 변수가 설정되었습니다.
사전 조건
- 모든 작업에 필요한 표준 전제 조건에 대해 알아보려면 NCache Read-Through 캐싱을 포함한 서버측 기능은 다음 페이지를 참조하십시오. 서버 측 API 전제 조건.
- 이것은 Microsoft Visual Studio를 사용하는 클래스 라이브러리 프로젝트여야 합니다.
- 에 있는지 확인 ReadThrough 공급자 배포 를 사용하여 NCache 관리센터.
- API 세부 정보는 다음을 참조하세요. IReadThruProvider, 소스에서 로드, 로드 데이터 유형에서 소스.
주의 사항
In NCache, Read-Through 캐싱 공급자는 캐시 수준에서 구성됩니다. 즉, 클러스터형 캐시의 경우 모든 노드에 공급자 구성과 배포된 어셈블리가 포함됩니다.
ReadThrough 캐싱 공급자에 대한 샘플 구현
다음 코드 샘플은 다음을 사용하여 Read-Through 캐싱 공급자를 구성하는 샘플 구현을 제공합니다. IReadThruProvider
.NET용 클래스와 ReadThruProvider
자바용 클래스. 그만큼 LoadFromSource
및 LoadDataTypeFromSource
메소드에는 개체가 캐시에서 발견되지 않은 경우 구성된 데이터 소스에서 각각 개체 또는 데이터 유형을 로드하는 논리가 포함되어 있습니다. 또한, GetConnectionString
구성된 데이터 원본에 대한 연결 문자열을 반환합니다.
public class SampleReadThruProvider : IReadThruProvider
{
private SqlConnection _connection;
//Perform tasks like allocating resources or acquiring connections
public void Init(IDictionary parameters, string cacheId)
{
object server = parameters["server"];
object userId = parameters["username"];
object password = parameters["password"];
object database = parameters["database"];
string connString = GetConnectionString(server.ToString(), database.ToString(), userId.ToString(), password.ToString());
if (connString != "")
_connection = new SqlConnection(connString);
try
{
_connection.Open();
}
catch (Exception ex)
{
//handle exception
}
}
// Responsible for loading an item from the external data source
public ProviderCacheItem LoadFromSource(string key)
{
// LoadFromDataSource loads data from data source
object value = LoadFromDataSource(key);
var cacheItem = new ProviderCacheItem(value);
return cacheItem;
}
//Responsible for loading bulk of items from the external data source
public IDictionary<string, ProviderCacheItem> LoadFromSource(ICollection<string> keys)
{
var dictionary = new Dictionary<string, ProviderCacheItem>();
try
{
foreach (string key in keys)
{
// LoadFromDataSource loads data from data source
dictionary.Add(key, new ProviderCacheItem(LoadFromDataSource(key)));
}
return dictionary;
}
catch (Exception exp)
{
// Handle exception
}
return dictionary;
}
// Adds ProviderDataTypeItem with enumerable data type
public ProviderDataTypeItem<IEnumerable> LoadDataTypeFromSource(string key, DistributedDataType dataType)
{
IEnumerable value = null;
ProviderDataTypeItem<IEnumerable> dataTypeItem = null;
switch (dataType)
{
case DistributedDataType.List:
value = new List<object>()
{
LoadFromDataSource(key)
};
dataTypeItem = new ProviderDataTypeItem<IEnumerable>(value);
break;
case DistributedDataType.Dictionary:
value = new Dictionary<string, object>()
{
{ key , LoadFromDataSource(key) }
};
dataTypeItem = new ProviderDataTypeItem<IEnumerable>(value);
break;
case DistributedDataType.Counter:
dataTypeItem = new ProviderDataTypeItem<IEnumerable>(1000);
break;
}
return dataTypeItem;
}
//Perform tasks associated with freeing, releasing, or resetting resources.
public void Dispose()
{
if (_connection != null)
{
_connection.Close();
}
}
private object LoadFromDataSource(string key)
{
object retrievedObject = null;
// Load item from your data source and populate retrieved Object
return retrievedObject;
}
private string GetConnectionString(string server, string database, string userName, string password)
{
string connectionString = null;
try
{
if (!string.IsNullOrEmpty(server))
{
connectionString = "Server=" + server + ";";
}
else
{
//Server name is empty
}
if (!string.IsNullOrEmpty(database))
{
connectionString = connectionString + "Database=" + database + ";";
}
else
{
//Database is empty;
}
if (!string.IsNullOrEmpty(userName))
{
connectionString = connectionString + "User ID=" + userName + ";";
}
else
{
connectionString = connectionString + "User ID=" + "" + ";";
}
if (!string.IsNullOrEmpty(password))
{
connectionString = connectionString + "Password=" + password + ";";
}
else
{
connectionString = connectionString + "Password=" + "" + ";";
}
}
catch (Exception exp)
{
// Handle exception
}
return connectionString;
}
// Deploy this class on cache
}
주의 사항
작업이 안전하도록 하려면 에 설명된 대로 응용 프로그램 내에서 잠재적인 예외를 처리하는 것이 좋습니다. 처리 실패.
추가 자료
NCache Read-Through에 대한 샘플 애플리케이션을 제공합니다. GitHub의.
도 참조
.그물: Alachisoft.NCache.실행 시간 네임 스페이스.
자바 : COM.alachisoft.ncache.runtime.datasourceprovider 네임 스페이스.