拡張可能なデータベースの依存関係の使用と実装を通知する
に加えて バルク拡張可能依存性, NCache 通知ベースの拡張依存関係または拡張データベース依存関係の通知と呼ばれるキャッシュ依存関係の別の方法を提供します。拡張可能なデータベース依存関係の通知では、クライアント側は、依存関係の背後にあるロジックを保持するプロバイダーと、その依存関係をいつ呼び出すかをデプロイする責任があります。
Note
この機能は、 NCache Enterprise for .NETサーバー.
次に、サーバーは、クライアントが提供するロジックを考慮して、データベースの依存関係に関連するメソッドのキャッシュを呼び出します。キャッシュは、Notify Extensible dependency の下にある項目と、それらの項目を削除する必要があるかどうかをチェックします。
通知ベースの拡張可能な依存関係では、クライアントは項目の依存関係をいつどのように呼び出すかをすべて制御できます。このようにして、非常に柔軟な方法でアイテムをキャッシュから期限切れにすることができます。
NCache 抽象クラスを提供します NotifyExtensibleDependency
、によって提供されるカスタム依存関係の拡張 NCache、この動作を実装します。
先端
参照する カスタムキャッシュの依存関係 によって提供されるすべてのキャッシュ依存関係のカスタムメソッドに精通するため NCache.
前提条件
- すべてを使用するために必要な標準的な前提条件について学習するには NCache サーバー側の機能については、指定されたページを参照してください サーバー側 API の前提条件.
- プロジェクトは、Visual Studioでクラスライブラリ(.dll)として実装する必要があります。 これはに展開されます NCache クラスタ。
- ユーザーとの強い関係を維持するために、 カスタム依存関係プロバイダーをデプロイする NCache 管理センター。
-
NotifyExtensibleDependency
クラスは キャッシュにデプロイ - APIの詳細については、以下を参照してください。 Iキャッシュ, キャッシュ項目, Add, insert, 削除します, カスタム依存関係, 依存関係の作成, ICustomDependencyProvider, NotifyExtensibleDependency, 依存関係変更ハンドラ.
Note
で実装されたメソッドに関する詳細情報を取得するには NotifyExtensibleDependency
を参照してください。 メソッド のセクションから無料でダウンロードできます。
ステップ1:NotifyExtensibleDependencyクラスを実装する
Notify Extensible Dependency に独自のロジックを導入する最初のステップは、 NotifyExtensibleDependency
とに提供されます。
独自の依存関係ロジックを使用して、顧客を Cosmos データベースに追加するとします。 アプリケーションで Cosmos DB を使用して Notification Extensible Dependency を実装する方法を次に示します。
[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:通知の拡張可能な依存関係を使用する
通知ベースの拡張依存関係が実装され、キャッシュにデプロイされると、アプリケーションで使用できるようになります。次のコードは、 インセット 通知ベースのカスタム依存関係を使用したメソッド。
Note
クライアントキャッシュの場合、ユーザーはこれらのアセンブリをクライアントキャッシュに明示的に展開する必要があります。
// 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()
アイテムがキャッシュに追加されるたびに、 Initialize
メソッドが呼び出され、キャッシュにデータ ソースとの安全な接続があるかどうかが確認されます。このメソッドが true を返した場合、キャッシュがデータ ソースとの安全な接続を確立できることを意味します。その後、削除が必要になるまで、その項目をキー/値ストアとキャッシュの依存関係リストに置きます。
ただし、このメソッドが false を返した場合は、データへのアクセスと監視に必要なデータ ソースがキャッシュで利用できないため、その項目がキャッシュから削除されることを示します。初期化メソッドは、すべての拡張可能な依存関係で使用できます。
DependencyChanged()
アイテムがキャッシュに正常に配置された後、そのアイテムへの依存関係を呼び出す方法を知る必要があります。 このため、代表者 DependencyChanged
使用されている。 このデリゲートのハンドラーがキャッシュにデプロイされます。 クライアントは、このデリゲートを呼び出してアイテムの依存関係を呼び出すタイミングを制御できます。 このデリゲートが呼び出されるたびに、呼び出された対象のアイテムがキャッシュから削除されます。
その他のリソース
NCache Notification Extensible Dependency のサンプル アプリケーションを提供します。 GitHubの.
も参照してください
。ネット: Alachisoft.NCache.Runtime.Dependency 名前空間