ライトスルー キャッシュ プロバイダーの構成と実装
ライトスルーキャッシュを使用するには、 IWriteThruProvider
.NET 用のインターフェイスと WriteThruProvider
Java用のインターフェース。 NCache このカスタム実装を使用して、データソースにデータを書き込みます。 参照してください 管理者ガイドのライトスルー キャッシュの構成 のガイドをご参照ください。
警告
ライトスルー キャッシュ プロバイダーが指定されている場合、 client.ncconf セクションに default-writethru-provider
タグとAPIを介して、APIのプロバイダーはで指定されたものを上書きします client.ncconf.
重要
Javaの場合、JARファイルをデプロイする前に、次のことを確認する必要があります。
- JDK 17 インストールされています。
- Javaの環境変数が設定されています。
前提条件
- すべてを使用するために必要な標準的な前提条件について学習するには NCache ライトスルー キャッシュを含むサーバー側の機能については、次のページを参照してください。 サーバー側 API の前提条件.
- これは、MicrosoftVisualStudioを使用したクラスライブラリプロジェクトである必要があります。
- ユーザーとの強い関係を維持するために、 WriteThroughプロバイダーをデプロイする NCache 管理センター。
- APIの詳細については、以下を参照してください。 IWriteThruProvider, データソースへの書き込み.
Note
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
}
Note
操作がフェイルセーフであることを保証するために、で説明されているように、アプリケーション内の潜在的な例外を処理することをお勧めします。 失敗の処理.
その他のリソース
NCache ライトスルー用のサンプル アプリケーションを提供します。 GitHubの.
も参照してください
。ネット: Alachisoft.NCache。ランタイム 名前空間
Java: comの。alachisoft.ncache.runtime.datasourceprovider 名前空間