Cache Data Dependency on Oracle Database
NCache provides OracleCacheDependency to register data change notifications with the Oracle database server. Hence you need to understand the limitations and working mechanisms of OracleDependency
, while using this dependency. See the following Oracle Documentation for further details.
NCache provides an Oracle Dependency to synchronize the cache with the Oracle database. Cache items expire if a command results in a change in the database. Oracle Dependency is available for Oracle database 11g or later. Also, install Oracle Data Providers for .NET (version 10.1.0.2.0+).
The database change notifications are object-based, firing change notifications upon any object row modification. Therefore, check ROWID to confirm if the altered row is the one for which the event was registered. ROWIDs cannot be retrieved unless explicitly included in the query. So, the user has to specifically include ROWID in the query that is being registered with OracleDependency
, otherwise, the change notification will be fired if any row is modified in the table.
When ROWID is included in a query such as: SELECT ROWID, UnitPrice FROM Products WHERE ProductID = {product.ProductID}
- NCache will save the ROWIDs of rows for which the change notification is registered. When it receives any change notification, NCache will compare the ROWIDs to determine whether the row changed is the one for which the ROWID is registered. Otherwise, NCache will have no way to check against this, and items with a change notification registered are removed if any row in the table changes.
Note
Before using Oracle Dependency, set up the Oracle Database Environment by referring to the Setup Oracle Database Environment section in the Admin Guide.
Prerequisites: Cache Data Dependency on Oracle Database
- Set up database environment before using Oracle Dependency.
- To learn about the standard prerequisites required to work with all NCache client-side features, please refer to the given page on Client-Side API Prerequisites.
- For API details, refer to: ICache, CacheItem, Dependency, OracleCacheDependency.
Adding Data with Oracle Database Dependency
Altering data after addition with Oracle Dependency will remove it from the cache. The OracleCacheDependency
is used to specify the dependency criteria, and then the item is added to the cache using the Add
or Insert
method.
The following example adds data with OracleCacheDependency
using the Insert
method. The Insert
method adds a new item with dependency, and if this data already exists in the cache, it overwrites its properties.
// Precondition: Cache is already connected
// 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);
}
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Add Data with Oracle Dependency using Stored Procedure
NCache lets you add data with an Oracle Dependency using a stored procedure. You can also specify the parameters to be passed along with the stored procedure, using the OracleCacheDependency
method.
The following example adds items to the cache with Oracle Dependency through the stored procedure using the Insert
method. The Insert
method adds a new item with dependency, and if this data already exists in the cache, it overwrites its properties.
// 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);
}
Additional Resources
NCache provides a sample application for Oracle dependency on GitHub.
See Also
.NET: Alachisoft.NCache.Runtime.Dependencies namespace.
Java: com.alachisoft.ncache.runtime.dependencies namespace.
Python: ncache.runtime.dependencies class.
Node.js: OracleCacheDependency class.