확장 가능한 데이터베이스 종속성 사용 및 구현 알림
이외에도 대량 확장 종속성, NCache 알림 기반 확장 가능 종속성 또는 확장 가능 데이터베이스 종속성 알림이라는 캐시 종속성에 대한 또 다른 방법을 제공합니다. 확장 가능 데이터베이스 종속성 알림에서 클라이언트 측은 종속성 뒤에 있는 논리를 보유하는 공급자를 배포하고 해당 종속성을 호출하는 시기를 담당합니다.
주의 사항
이 기능은 NCache Enterprise for .NET 서버.
그런 다음 서버는 클라이언트가 제공하는 논리를 고려하여 데이터베이스 종속성 관련 메서드에 대한 캐시를 호출합니다. 캐시는 확장 가능 종속성 알림 아래의 항목을 확인하고 해당 항목을 제거해야 하는지 여부를 확인합니다.
알림 기반 확장 가능 종속성에서는 클라이언트가 항목에 대한 종속성을 호출하는 시기와 방법을 모두 제어할 수 있습니다. 이러한 방식으로 항목은 매우 유연한 방식으로 캐시에서 만료될 수 있습니다.
NCache 추상 클래스 제공 NotifyExtensibleDependency
, 다음에서 제공하는 사용자 지정 종속성의 확장 NCache, 이 동작을 구현합니다.
팁
인용하다 사용자 지정 캐시 종속성 에서 제공하는 모든 캐시 종속성 사용자 정의 방법에 대해 알아보기 NCache.
사전 조건
- 모든 작업에 필요한 표준 전제 조건에 대해 알아보려면 NCache 서버 측 기능은 다음 페이지를 참조하십시오. 서버 측 API 전제 조건.
- 프로젝트는 Visual Studio에서 클래스 라이브러리(.dll)로 구현되어야 합니다. 이것은 다음에 배포됩니다 NCache 클러스터.
- 에 있는지 확인 사용자 지정 종속성 공급자 배포 를 사용하여 NCache 관리센터.
- XNUMXD덴탈의
NotifyExtensibleDependency
수업은 반드시 캐시에 배포 - API 세부 정보는 다음을 참조하세요. 아이캐시, 캐시 아이템, 추가, 삽입하다, 제거, 사용자 정의 종속성, 의존성 생성, ICustomDependencyProvider, 확장 가능한 종속성 알림, 종속성ChangedHandler.
주의 사항
구현된 메서드에 대한 자세한 정보를 얻으려면 NotifyExtensibleDependency
, 행동 양식 안내
1단계: NotifyExtensibleDependency 클래스 구현
Notify Extensible Dependency에 대한 고유한 논리를 도입하는 첫 번째 단계는 다음을 상속하는 것입니다. NotifyExtensibleDependency
클래스입니다.
고유한 종속성 논리를 사용하여 고객을 Cosmos 데이터베이스에 추가한다고 가정해 보겠습니다. 애플리케이션에서 Cosmos DB를 사용하여 알림 확장 가능 종속성을 구현하는 방법은 다음과 같습니다.
[Serializable]
public class NotificationDependency<T> : NotifyExtensibleDependency
{
// Class parameters
public NotificationDependency(/* */)
{
// Assign values to class members
}
public override bool Initialize()
{
// Register dependency against key
RegisterDependency(_key, this);
return true;
}
public bool RegisterDependency(string key, NotifyExtensibleDependency dependency)
{
lock (this)
{
// Your logic here
dependencies.Add(key, dependency);
}
return true;
}
public void OnFeedChange(params object[] args)
{
// Find the matching dependency and fire its DependencyChangedEvent
var key = args[0];
var notifyExtensibleDep = (dependencies[key] as NotifyExtensibleDependency);
notifyExtensibleDep?.DependencyChanged.Invoke(this);
// Remove the matched keys from cache
lock (dependencies)
{
dependencies.Remove(key);
}
}
protected override void DependencyDispose()
{
// Dispose off resources
}
// This class is to be deployed on NCache
}
2단계: NotifyCustomDependencyProvider 구현
애플리케이션에서 Notify Extensible Dependency 제공자를 구현하려면 다음 코드 스니펫을 사용하십시오.
public class NotifyCustomDependencyProvider : ICustomDependencyProvider
{
private string cacheName;
private string monitoredUri;
private string authKey;
private string databaseName;
public void Init(IDictionary<string, string> parameters, string cacheName)
{
// Initialize cache and class parameters
}
public NotificationDependency CreateDependency(string key, IDictionary<string, string> dependencyParameters)
{
string customerId="";
string monitoredCollection = "";
string leaseCollection = "";
if (dependencyParameters != null)
{
if (dependencyParameters.ContainsKey("Key"))
customerId = dependencyParameters["Key"];
if (dependencyParameters.ContainsKey("MonitoredCollectionName"))
monitoredCollection = dependencyParameters["MonitoredCollectionName"];
if (dependencyParameters.ContainsKey("LeaseCollectionName"))
leaseCollection = dependencyParameters["LeaseCollectionName"];
// Create notify extensible dependency
NotificationDependency<Customer> cosmosDbDependency = new NotificationDependency<Customer>(customerId,
monitoredUri, authKey, databaseName, monitoredCollection, databaseName, leaseCollection);
return cosmosDbDependency;
}
else
{
// Dependency parameters not found
}
}
public void Dispose ()
{
// Dispose off all resources
}
}
3단계: 캐시에 구현 배포
이 클래스와 다른 모든 종속 어셈블리를 다음 위치에 배포합니다. NCache 를 참조하여 공급자 배포 도움이 필요하면 관리자 가이드를 참조하세요.
4단계: 알림 확장 종속성 사용
알림 기반 확장 가능 종속성을 구현하고 캐시에 배포하면 애플리케이션에서 사용할 수 있습니다. 다음 코드는 끼워 넣다 알림 기반 사용자 지정 종속성이 있는 메서드입니다.
주의 사항
클라이언트 캐시의 경우 사용자는 이러한 어셈블리를 클라이언트 캐시에 명시적으로 배포해야 합니다.
// Specify the connection credentials
string endPoint = ConfigurationManager.AppSettings["EndPoint"];
string authKey = ConfigurationManager.AppSettings["AuthKey"];
string monitoredCollection = ConfigurationManager.AppSettings["MonitoredCollection"];
string leaseCollection = ConfigurationManager.AppSettings["LeaseCollection"];
string databaseName = ConfigurationManager.AppSettings["DatabaseName"];
string providerName = ConfigurationManager.AppSettings["ProviderName"];
// Fetch a sample customer from the database
Customer customer = LoadCustomerFromDatabase(customerId);
// Specify the unique key of the item
string key = "Customer#" + customer.Id ;
// Create dictionary for dependency parameters
IDictionary<string, string> param = new Dictionary<string, string>();
param.Add("CustomerID", customer.Id);
param.Add("EndPOint", endPoint);
param.Add("AuthKey", authKey);
param.Add("MonitoredCollection", monitoredCollection);
param.Add("LeaseCollection", leaseCollection);
param.Add("DatabaseName", databaseName);
//Creating notification dependency
CustomDependency cosmosDbDependency = new CustomDependency(providerName, param);
// Create a cacheItem
var cacheItem = new CacheItem(customer);
cacheItem.Dependency = cosmosDbDependency;
// Add cacheItem to the cache with notification dependency
cache.Insert(key, cacheItem);
행동 양식
초기화 ()
아이템이 캐시에 추가될 때마다 Initialize
캐시가 데이터 소스와 안전하게 연결되어 있는지 여부를 확인하기 위해 메서드가 호출됩니다. 이 메서드가 true를 반환하면 캐시가 데이터 소스와 보안 연결을 설정할 수 있다는 의미입니다. 그런 다음 해당 항목을 제거해야 할 때까지 키-값 저장소와 캐시의 종속성 목록에 배치합니다.
그러나 이 메서드가 false를 반환하는 경우 데이터에 액세스하고 모니터링하는 데 필요한 데이터 소스를 캐시에서 사용할 수 없으므로 해당 항목이 캐시에서 제거된다는 의미입니다. 초기화 방법은 모든 확장 가능한 종속성에 사용할 수 있습니다.
종속성 변경()
항목이 캐시에 성공적으로 배치되면 항목에 대한 종속성을 호출하는 방법을 알아야 합니다. 이러한 이유로 대의원은 DependencyChanged
사용. 이 대리자의 처리기가 캐시에 배포됩니다. 클라이언트는 이 대리자를 호출하고 항목에 대한 종속성을 호출할 시기를 제어합니다. 이 대리자가 호출될 때마다 캐시에서 호출된 항목을 제거합니다.
추가 자료
NCache Notification Extensible Dependency에 대한 샘플 애플리케이션을 제공합니다. GitHub의.
도 참조
.그물: Alachisoft.NCache.런타임.종속성 네임 스페이스.