Try Playground
Show / Hide Table of Contents

Cache Data Dependency on Oracle Database [Deprecated]

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.

  • Special Considerations Using Query Notifications
  • OracleDependency

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

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
  • 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.
  • 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: Cache, CacheItem, setDependency, OracleCacheDependency.
  • 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: Cache, CacheItem, OracleCacheDependency, insert, set_dependency.
  • 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: Cache, CacheItem, setDependency, OracleCacheDependency.
  • Create a new Console Application.
  • Make sure that the data being added is serializable.
  • Add NCache References by locating %NCHOME%\NCache\bin\assembly\4.0 and adding Alachisoft.NCache.Web and Alachisoft.NCache.Runtime as appropriate.
  • Include the Alachisoft.NCache.Runtime.Dependencies namespace in your application.

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.

  • .NET
  • Java
  • Python
  • Node.js
  • Legacy API
// 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);
}
// Precondition: Cache is already connected
// 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);
}
# Precondition: Cache is already connected
# 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
// Precondition: Cache is already connected
// 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
// Create object to be added with dependency
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
string key = "Product:" + product.ProductID;

//Creating OracleCacheDependency
string connectionString = "User Id=scott;Password=tiger;Data Source=oracle";
string query = "SELECT rowID, productID FROM dbo.Products WHERE productID = 1001";

OracleCacheDependency oracleDependency = new OracleCacheDependency(connectionString, query);

//Adding cache item "Product:1001" with SqlDependency
cache.Insert(key, product, oracleDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);

//Modify Product record in database while program waits...
Thread.Sleep(5000);

//... and then check for its existence
Object item = cache.Get(key);
if (item == null)
{
      // item removed successfully
}
else
{
      // item not removed successfully
}
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.

  • .NET
  • Java
  • Python
  • Node.js
// 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);
}
# 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
// 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 upon
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();

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.

In This Article
  • Prerequisites: Cache Data Dependency on Oracle Database
  • Adding Data with Oracle Database Dependency
  • Add Data with Oracle Dependency using Stored Procedure
  • Additional Resources
  • See Also

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top