缓存 Oracle 数据库上的数据依赖关系
NCache 提供 Oracle缓存依赖 用于与 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 将无法对此进行检查,并且如果表中的任何行发生更改,则注册了更改通知的项目将被删除。
先决条件:缓存数据对 Oracle 数据库的依赖性
添加具有 Oracle 数据库依赖性的数据
添加 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
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
使用存储过程添加具有 Oracle 依赖关系的数据
NCache 允许您使用存储过程提供对项目的 Oracle 依赖关系。 您还可以使用以下命令指定要与存储过程一起传递的参数 OracleCacheDependency
方法。
以下示例通过存储过程使用 Oracle Dependency 将项目添加到缓存 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上.
参见
.NET: Alachisoft.NCache.运行时.依赖项 命名空间。
Java的: COM。alachisoft.ncache.runtime.dependency 命名空间。
节点.js: Oracle缓存依赖 类。
Python: ncache.runtime.dependency 类。