Notify Extensible Database Dependency Usage and Implementation
此外 批量可扩展依赖, NCache provides another method for cache dependency called Notification Based Extensible Dependency or Notify Extensible Database Dependency. In Notify Extensible Database Dependency, the client side is responsible for deploying a provider that holds the logic behind the dependency and when to invoke that dependency.
备注
此功能仅在 NCache Enterprise .NET 服务器.
The server then invokes the cache for database dependency-related methods considering the logic provided by the client. The cache checks for items under Notify Extensible Dependency and whether they need to be removed or not.
在基于通知的可扩展依赖项中,客户端可以完全控制何时以及如何调用项目的依赖项。 通过这种方式,可以以极其灵活的方式使缓存中的项目过期。
NCache 提供一个抽象类 NotifyExtensibleDependency
,由提供的自定义依赖项的扩展 NCache,实现了这种行为。
Tips:
请参阅 自定义缓存依赖 熟悉由 提供的所有缓存依赖自定义方法 NCache.
先决条件
备注
获取有关实现的方法的详细信息 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 通过参考 部署提供者 在管理员指南中寻求帮助。
第三步:使用通知可扩展依赖
一旦基于通知的可扩展依赖关系被实现并部署在缓存上,它就可以在您的应用程序中使用了。 下面的代码展示了如何使用添加数据 插页 具有基于通知的自定义依赖项的方法。
备注
在客户端缓存的情况下,用户需要在客户端缓存上显式部署这些程序集。
// 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 提供通知可扩展依赖的示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache.运行时.依赖项 命名空间。