直写式缓存提供程序配置和实现
要使用直写缓存,您需要实现 IWriteThruProvider
.NET 的接口和 WriteThruProvider
Java的接口。 NCache 将使用此自定义实现在数据源上写入数据。 请参阅 在管理员指南中配置直写式缓存 以获得更多细节。
警告
如果在中指定了直写式缓存提供程序 客户端.ncconf ,在 default-writethru-provider
标记并通过 API,然后 API 中的提供者将覆盖中指定的提供者 客户端.ncconf.
重要
对于 Java,在部署 JAR 文件之前,您需要确保:
- JDK 17 安装。
- Java 的环境变量已设置。
先决条件
- 了解与所有人员合作所需的标准先决条件 NCache 服务器端功能,包括直写式缓存,请参阅给定页面 服务器端 API 先决条件.
- 这应该是一个使用 Microsoft Visual Studio 的类库项目。
- 确保 部署 WriteThrough 提供程序 使用 NCache 管理中心。
- 有关 API 详细信息,请参阅: 写通提供者, 写入数据源.
备注
In NCache,直写提供程序在缓存级别上配置,即,对于集群缓存,所有节点都将包含提供程序配置及其部署的程序集。
示例实现
以下代码示例提供了示例实现 IWriteThruProvider
.NET 接口和 WriteThruProvider
分别用于Java。 这 WriteToDataSource
方法包含对配置的数据源执行写入操作的逻辑。使用它,您可以将对象、批量对象和数据类型从缓存写入数据源。
实现并部署此提供程序后,您可以直接调用 NCache 用于对数据源的写操作。 NCache 使用 WriteToDataSource
指定数据访问类中的方法对数据源进行写入操作。
// Contains methods used to save/update an object to the master data source.
public class SampleWriteThruProvider : IWriteThruProvider
{
// Object used to communicate with the Data source.
private SqlConnection _connection;
// Perform tasks like allocating resources or acquiring connections
public void Init(IDictionary parameters, string cacheId)
{
try
{
string connString = GetConnectionString(parameters);
if (!string.IsNullOrEmpty(connString))
{
_connection = new SqlConnection(connString);
_connection.Open();
}
}
catch (Exception ex)
{
// Handle exception
}
}
// Perform tasks associated with freeing, releasing, or resetting resources.
public void Dispose()
{
if (_connection != null)
{
_connection.Close();
}
}
//Responsible for write operations on data source
public OperationResult WriteToDataSource(WriteOperation operation)
{
ProviderCacheItem cacheItem = operation.ProviderItem;
Product product = cacheItem.GetValue<Product>();
switch (operation.OperationType)
{
case WriteOperationType.Add:
// Insert logic for any Add operation
break;
case WriteOperationType.Delete:
// Insert logic for any Delete operation
break;
case WriteOperationType.Update:
// Insert logic for any Update operation
break;
}
// Write Thru operation status can be set according to the result.
return new OperationResult(operation, OperationResult.Status.Success);
}
public ICollection<OperationResult> WriteToDataSource(ICollection<WriteOperation> operations)
{
var operationResult = new List<OperationResult>();
foreach (WriteOperation operation in operations)
{
ProviderCacheItem cacheItem = operation.ProviderItem;
Product product = cacheItem.GetValue<Product>();
switch (operation.OperationType)
{
case WriteOperationType.Add:
// Insert logic for any Add operation
break;
case WriteOperationType.Delete:
// Insert logic for any Delete operation
break;
case WriteOperationType.Update:
// Insert logic for any Update operation
break;
}
// Write Thru operation status can be set according to the result
operationResult.Add(new OperationResult(operation, OperationResult.Status.Success));
}
return operationResult;
}
public ICollection<OperationResult> WriteToDataSource(ICollection<DataTypeWriteOperation> operations)
{
var operationResult = new List<OperationResult>();
foreach (DataTypeWriteOperation operation in operations)
{
var list = new List<Product>();
ProviderDataTypeItem<object> cacheItem = operation.ProviderItem;
Product product = (Product)cacheItem.Data;
switch (operation.OperationType)
{
case DatastructureOperationType.CreateDataType:
// Insert logic for creating a new List
IList myList = new List<Product>();
myList.Add(product.ProductID);
break;
case DatastructureOperationType.AddToDataType:
// Insert logic for any Add operation
list.Add(product);
break;
case DatastructureOperationType.DeleteFromDataType:
// Insert logic for any Remove operation
list.Remove(product);
break;
case DatastructureOperationType.UpdateDataType:
// Insert logic for any Update operation
list.Insert(0, product);
break;
}
// Write Thru operation status can be set according to the result.
operationResult.Add(new OperationResult(operation, OperationResult.Status.Success));
}
return operationResult;
}
// Parameters specified in Manager are passed to this method
// These parameters make the connection string
private string GetConnectionString(IDictionary parameters)
{
string connectionString = string.Empty;
string server = parameters["server"] as string, database = parameters["database"] as string;
string userName = parameters["username"] as string, password = parameters["password"] as string;
try
{
connectionString = string.IsNullOrEmpty(server) ? "" : "Server=" + server + ";";
connectionString = string.IsNullOrEmpty(database) ? "" : "Database=" + database + ";";
connectionString += "User ID=";
connectionString += string.IsNullOrEmpty(userName) ? "" : userName;
connectionString += ";";
connectionString += "Password=";
connectionString += string.IsNullOrEmpty(password) ? "" : password;
connectionString += ";";
}
catch (Exception exp)
{
// Handle exception
}
return connectionString;
}
// Deploy this class on cache
}
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
更多资讯
NCache 提供了 Write-through 的示例应用程序 GitHub上.
参见
.NET: Alachisoft.NCache。运行 命名空间。
Java的: COM。alachisoft.ncache.runtime.datasourceprovider 命名空间。