Oracleデータベースへのキャッシュデータの依存関係
NCache は、大阪で OracleCacheDependency Oracle との通知ベースの依存関係の場合。 内部的に、 NCache 使用されます OracleDependency
データ変更通知を Oracle データベース サーバーに登録します。 したがって、の制限と動作メカニズムを理解する必要があります。 OracleDependency
、この依存関係を使用している間。 詳細については、次の Oracle ドキュメントを参照してください。
NCache は、キャッシュを Oracle データベースと同期するための Oracle 依存関係を提供します。 コマンドによってデータベースが変更されると、キャッシュ項目は期限切れになります。 Oracle 依存関係は、Oracle データベース 11g 以降で使用できます。 また、Oracle Data Providers for .NET (バージョン 10.1.0.2.0+) をインストールします。
データベース変更通知はオブジェクトベースであり、オブジェクト行が変更されると変更通知が発行されます。 したがって、ROWID をチェックして、変更された行がイベントが登録された行であるかどうかを確認します。 ROWID は、クエリに明示的に含まれない限り取得できません。 したがって、ユーザーは登録されるクエリに ROWID を明確に含める必要があります。 OracleDependency
そうでない場合、テーブル内の行が変更された場合に変更通知が発行されます。
ROWID が次のようなクエリに含まれている場合: SELECT ROWID, UnitPrice FROM Products WHERE ProductID = {product.ProductID}
- NCache 変更通知が登録されている行のROWIDが保存されます。 変更通知を受け取ると、 NCache ROWID を比較して、変更された行が ROWID が登録されている行であるかどうかを判断します。 さもないと、 NCache これをチェックする方法はなく、テーブル内の行が変更されると、変更通知が登録されている項目は削除されます。
Note
Oracle 依存関係を使用する前に、以下を参照して Oracle データベース環境をセットアップしてください。 Oracleデータベース環境のセットアップ 管理者ガイドのセクションを参照してください。
前提条件: Oracle データベースへのキャッシュ データの依存関係
Oracle Database依存関係のあるデータの追加
Oracle 依存関係を使用して追加した後にデータを変更すると、そのデータはキャッシュから削除されます。 OracleCacheDependency
を使用して依存関係の基準を指定し、項目を使用してキャッシュに追加します。 Add
or Insert
方法。
次の例では、 OracleCacheDependency
Insert
方法。 ザ Insert
このメソッドは依存関係のある新しい項目を追加し、このデータがキャッシュにすでに存在する場合は、そのプロパティを上書きします。
// Creating connection string to get connected with database.
string connectionString = "your_connection_string_here";
// Getting products from database.
List<Product> products = FetchProductFromDB();
foreach (Product product in products)
{
string productKey = $"Product: {product.ProductID}";
// Creating an Oracle dependency on the UnitPrice of product. Whenever the UnitPrice changes, the product is removed from the cache.
string query = $"SELECT ROWID, UnitPrice FROM Products WHERE ProductID = {product.ProductID}";
// Creating dependency on fetched products.
OracleCacheDependency dependency = new OracleCacheDependency(connectionString, query);
CacheItem productItem = new CacheItem(product);
// Adding Dependency to product item
productItem.Dependency = dependency;
// Adding CacheItem in cache
cache.Add(productKey, productItem);
}
// Create a connection string to get connected with the database
String connectionString = "your_connection_string_here";
// Getting products from the database
List<Product> products = fetchProductFromDB();
for (Product product : products)
{
String productKey = "Product:" + product.getProductID();
// Creating an Oracle dependency on the UnitPrice of the product.
// Whenever the UnitPrice changes, the product is removed from the cache.
String query = "SELECT ROWID, UnitPrice FROM Products WHERE ProductID = " + product.getProductID();
// Creating dependency on fetched products.
OracleCacheDependency dependency = new OracleCacheDependency(connectionString, query);
CacheItem productItem = new CacheItem(product);
// Adding Dependency to product item
productItem.setDependency(dependency);
// Adding CacheItem in cache
cache.insert(productKey, productItem);
System.out.println("Product added to cache: " + productKey);
}
// Creating a connection string to establish connection with the database
var connection = new ActiveXObject("adodb.connection");
let connString = "...";
connection.Open(connString);
var result = new ActiveXObject("ADODB.Recordset");
let query = "Select ROWID, ProductId, ProductName, UnitPrice FROM dbo.Products WHERE ProductId = ?";
let queryCommand = new QueryCommand(query);
queryCommand.getParameters().put("CustomerID", this.customerId);
result.Open(query, connection);
// Get product from database against productId
let product = this.fetchProductFromDb(this.productId);
// Create unique cache key for this product
let key = "Product:" + product.ProductID;
// Create oracle dependency
var oracleCacheDependency = new ncache.OracleCacheDependency(connString, query);
// Create new cache item and add oracle dependency to it
let cacheItem = new ncache.CacheItem(product);
cacheItem.setDependency(oracleCacheDependency);
// Add cache item in the cache item with oracle dependency
this.cache.insert(key, cacheItem);
result.close();
connection.close();
// For successful addition of item with Oracle Dependency
// Update the record in the database and check if key is present
# Creating a connection string to establish connection with the database
conn_string = "your_connection_string"
query = "SELECT ROWID, ProductId, ProductName, UnitPrice FROM dbo.Products WHERE ProductId = ?"
query_command = ncache.QueryCommand(query)
query_command.set_parameters({"CustomerID": "ALFKI"})
# Get product from database against productId
product = fetch_product_from_db("1001")
# Create unique cache key for this product
key = "Product:" + product.get_product_id()
# Create oracle dependency
oracle_cache_dependency = ncache.OracleCacheDependency(conn_string, query)
# Create new cache item and add oracle dependency to it
cache_item = ncache.CacheItem(product)
cache_item.set_dependency(oracle_cache_dependency)
# Add cache item in the cache item with oracle dependency
cache.insert(key, cache_item)
# For successful addition of item with Oracle Dependency
# Update the record in the database and check if key is present
Note
操作がフェイルセーフであることを保証するために、で説明されているように、アプリケーション内の潜在的な例外を処理することをお勧めします。 失敗の処理.
ストアドプロシージャを使用してOracleDependencyでデータを追加する
NCache ストアド プロシージャを使用して項目に Oracle 依存関係を提供できます。 また、ストアド プロシージャと一緒に渡されるパラメータを指定することもできます。 OracleCacheDependency
方法。
次の例では、ストアド プロシージャを使用して、Oracle 依存関係を持つキャッシュに項目を追加します。 Insert
方法。 ザ Insert
このメソッドは依存関係のある新しい項目を追加し、このデータがキャッシュにすでに存在する場合は、そのプロパティを上書きします。
// Creating connection string to get connected with database.
string connectionString = "your_connection_string_here";
string spGetUnitPriceByProductID = "sp_GetUnitPriceByProductID";
// Getting products from database.
List<Product> products = FetchProductFromDB();
foreach (Product product in products)
{
string productKey = $"Product: {product.ProductID}";
// Creating Param to be passed in stored procedure dictionary.
OracleCmdParams paramProductID = new OracleCmdParams
{
Type = OracleCmdParamsType.Int32,
Value = product.ProductID
};
// Creating stored procedure params.
Dictionary<string, OracleCmdParams> parameters = new Dictionary<string, OracleCmdParams>();
parameters.Add("@ProductID", paramProductID);
CacheItem productItem = new CacheItem(product);
// Creating an Oracle dependency on the UnitPrice of product. Whenever the UnitPrice changes, the product is removed from the cache.
OracleCacheDependency dependency = new OracleCacheDependency(connectionString, spGetUnitPriceByProductID, OracleCommandType.StoredProcedure, parameters);
// Adding Dependency to product item
productItem.Dependency = dependency;
// Adding CacheItem in cache
cache.Add(productKey, productItem);
}
// Create a connection string to get connected with the database
String connectionString = "your_connection_string_here";
String spGetUnitPriceByProductID = "sp_GetUnitPriceByProductID";
// Getting products from the database
List<Product> products = fetchProductFromDB();
for (Product product : products)
{
String productKey = "Product:" + product.getProductID();
// Creating Param to be passed in the stored procedure dictionary.
OracleCmdParams paramProductID = new OracleCmdParams();
paramProductID.setType(OracleCmdParamsType.Int32);
paramProductID.setValue(product.getProductID());
// Creating stored procedure params using the constructor.
Map<String, OracleCmdParams> parameters = new HashMap<>();
parameters.put("@ProductID", paramProductID);
CacheItem productItem = new CacheItem(product);
// Creating an Oracle dependency on the UnitPrice of the product.
// Whenever the UnitPrice changes, the product is removed from the cache.
OracleCacheDependency dependency = new OracleCacheDependency(connectionString, spGetUnitPriceByProductID, OracleCommandType.StoredProcedure, parameters);
// Adding Dependency to the product item.
productItem.setDependency(dependency);
// Adding CacheItem to the cache.
cache.insert(productKey, productItem);
System.out.println("Product added to cache: " + productKey);
}
// This is an async method
// Create connection with the database
var connection = new ActiveXObject("adodb.connection");
let connString = "your_connection_string_here";
connection.Open(connString);
// Name of the stored procedure the item is dependent on
let storedProcedureName = "GetProductByID";
// Specify the Product ID passed as parameters
let oracleCmdParamsMap = new Map();
oracleCmdParamsMap.set("ProductID", this.productId);
// Create oracle dependency with stored procedure
var oracleCacheDependency = new ncache.OracleCacheDependency(connString, storedProcedureName, OracleCommandType.StoredProcedure, oracleCmdParamsMap);
// Get product from database against productId
let product = this.fetchProductFromDb(this.productId);
// Generate a unique cache key for this product
let key = "Product:" + product.ProductID;
// Create a new cache item and add oracle dependency to it
let cacheItem = new ncache.CacheItem(product);
cacheItem.setDependency(oracleCacheDependency);
// Add cache item in the cache with oracle dependency
await this.cache.insert(key, cacheItem);
// For successful addition of item with Oracle Dependency
// Update the record in the database and check if key is present
connection.close();
# Create a connection string to connect with the database
conn_string = "your_connection_string_here"
# Name of the stored procedure the item is dependent upon
stored_procedure_name = "GetProductByID"
# Specify the Product ID passed as parameters
oracle_cmd_params = {"ProductID": "ALFKI"}
# Create oracle dependency with stored procedure
oracle_cache_dependency = ncache.OracleCacheDependency(conn_string, stored_procedure_name, ncache.OracleCommandType.STORED_PROCEDURE, oracle_cmd_params)
# Get product from database against productId
product = fetch_product_from_db("ALFKI")
# Generate a unique cache key for this product
key = "Product:" + product.get_product_id()
# Create a new cache item and add oracle dependency to it
cache_item = ncache.CacheItem(product)
cache_item.set_dependency(oracle_cache_dependency)
# Add cache item in the cache with oracle dependency
cache.insert(key, cache_item)
# For successful addition of item with Oracle Dependency
# Update the record in the database and check if key is present
その他のリソース
NCache Oracle 依存関係のサンプル アプリケーションを提供します。 GitHubの.
も参照してください
。ネット: Alachisoft.NCache.Runtime.Dependency 名前空間
Java: comの。alachisoft.ncache.runtime.dependency 名前空間
Node.js: OracleCacheDependency とに提供されます。
Python: ncache.runtime.dependency とに提供されます。